Arduino: user-defined type "does not name a type"

19,905

Solution 1

It looks like you need to set it up like libraries/Jerry/Jerry.h and libraries/Button/Button.h in the same folder as your sketch.

Check out: http://arduino.cc/en/Hacking/LibraryTutorial

First, make a Morse directory inside of the libraries sub-directory of your sketchbook directory. Copy or move the Morse.h and Morse.cpp files into that directory. Now launch the Arduino environment. If you open the Sketch > Import Library menu, you should see Morse inside. The library will be compiled with sketches that use it. If the library doesn't seem to build, make sure that the files really end in .cpp and .h (with no extra .pde or .txt extension, for example). Let's see how we can replicate our old SOS sketch using the new library:

#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);
}

Solution 2

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. :-)

Share:
19,905
STN
Author by

STN

Updated on June 04, 2022

Comments

  • STN
    STN about 2 years

    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.