3

I am having some trouble with writing classes in Arduino. Here is a class called "Jerry." It contains three instances of user-defined classes called Mouth, Move, and Injection. The Arduino IDE complains 'Mouth' does not name a type, and 'Move' does not name a type. How can I make Arduino recognize Mouth and Move as valid types?

   #ifndef JERRY_H
   #define JERRY_H
   include "Button.h"
   include "Injection.h"

   class Jerry 
   {

        protected:
            int BGL;
            double protein_conc;

        public:
            Jerry(int, int);
            Mouth mth;
            Move mv;
            Injection inj;
            volatile long prev_interrupt_time;
   };

   #endif // JERRY_H

(# signs removed because it screws up the formatting in StackOverflow)

The Mouth, Move, and Injection class declarations are below:

    #ifndef BUTTON_H
    #define BUTTON_H
    include "Jerry.h"

    class Button
    {
        public:
            void setPin(int);
            int getPin();
            bool check(Jerry);
            virtual void run();
        protected:
            int pin;
   };

    class Mouth : public Button
    {
        public:
            int detectFood();
            void run();
    };

    class Move : public Button
    {
        public:
            bool checkLaughing();
            void runLaughing();
            bool checkSleep();
    };

    #endif // BUTTON_H

    #ifndef INJECTION_H
    #define INJECTION_H


    class Injection
    {
         public:
            void setPin(int, int, int, int);
            void checkInjection(); 

            void checkInjectionSite(); 
         protected:
            int pin1;
            int pin2;
            int pin3;
            int pin4;
    };

    #endif // INJECTION_H

any help would be greatly appreciated.

4

2 に答える 2

3

There are two circular dependencies.

  1. class definition of Jerry uses Mouth, Mouth inherits Button, Button expects class Jerry to be already defined.
  2. class definition of Jerry uses Move, Move inherits Button, Button expects class Jerry to be already defined.

To sort these out, use forward declarations. :-)

于 2013-04-19T00:04:36.330 に答える
1

libraries/Jerry/Jerry.hスケッチとlibraries/Button/Button.h同じフォルダに設定する必要があるようです。

チェックアウト: http ://arduino.cc/en/Hacking/LibraryTutorial

まず、sketchbookディレクトリのlibrariesサブディレクトリ内にMorseディレクトリを作成します。Morse.hファイルとMorse.cppファイルをそのディレクトリにコピーまたは移動します。次に、Arduino環境を起動します。[スケッチ]>[ライブラリのインポート]メニューを開くと、内部にMorseが表示されます。ライブラリは、それを使用するスケッチでコンパイルされます。ライブラリがビルドされていないように見える場合は、ファイルが実際に.cppおよび.hで終わっていることを確認してください(たとえば、余分な.pdeまたは.txt拡張子はありません)。新しいライブラリを使用して古いSOSスケッチを複製する方法を見てみましょう。

#include <Morse.h>

Morse morse(13);

void setup()
{
}

void loop()
{
  morse.dot(); morse.dot(); morse.dot();
  morse.dash(); morse.dash(); morse.dash();
  morse.dot(); morse.dot(); morse.dot();
  delay(3000);
}
于 2012-06-22T00:02:43.207 に答える