enum type problem, 'foo' does not name a type

I'm trying to use an enum to represent some values. The compiler doesn't seem to recognize it:

enum foo { bar };
foo baz(){}

error: 'foo' does not name a type.

How do I declare an enum 'foo' such that I can use it as a function return type?

Ah ha: Arduino Playground - HomePage

types.h

enum foo { bar };

test.pde

#include "types.h"
foo baz(){}

Am I missing something or did you just split your code into a .PDE and a .H file? Otherwise the code is the same. Did it not work the first time?

It's my understanding that the IDE parses the PDE into the various C++ elements then puts them back together before feeding them to the compiler. It's also my understanding that the IDE reorders some of the parts. In this case, it apparently puts function declarations before enum declarations. The work-around is to move the enum declaration to a seperate header file ensuring the whole mess is fed to the compiler in the correct order.

  • Brian

Whoa...that's a lot of parsing and mucking around with a program. Are you sure the IDE is really that "smart" (with respect to parsing and separating out type declarations from function declarations)?

And why would it put what is essentially a type declaration before a function declaration??? That doesn't make sense to me.

It does rework the PDE a bit. Here is an example.

Here is a PDE and a header file with two enums declared. The first enum is in a header file I've called types.h. The second is directly in the PDE.

types.h

enum Direction1 {d1North, d1South, d1East, d1West};

enumTest.pde

#include "types.h"
enum Directions2 {d2North, d2South, d2East, d2West};

Direction1 aDirection = d1North;

void setHeading( Direction1 newDir ){}

void setup(){}

void loop(){}

When this is compiled the IDE has to generate some valid files for the compiler. This requires the addition of a main() and a little work to ensure that the order the user has declared the functions in doesn't matter. To do this it creates forward definitions for each function and inserts them near the top of the PDE, right after my #includes.

Unfortunately, this means that the forward declarations come before the enum!

#include "types.h"
#include "WProgram.h"
void setHeading( Direction1 newDir );
void setup();
void loop();
enum Directions2 {d2North, d2South, d2East, d2West};

Direction1 aDirection = d1North;

void setHeading( Direction1 newDir ){}

void setup(){}

void loop(){}

int main(void)
{
      init();

      setup();
    
      for (;;)
            loop();
        
      return 0;
}

You could try to be clever and put your enum above your first #include, like this:

enum BTest {btone, bttwo};
#include "types.h"
enum Directions2 {d2North, d2South, d2East, d2West};

Direction1 aDirection = d1North;

void baz( BTest bar ){}

void setHeading( Direction1 newDir ){}

void setup(){}

void loop(){}

But in this case you can't win for losing, the result is this:

#include "WProgram.h"
void baz( BTest bar );
void setHeading( Direction1 newDir );
void setup();
void loop();
enum BTest {btone, bttwo};
#include "types.h"
enum Directions2 {d2North, d2South, d2East, d2West};

Direction1 aDirection = d1North;

void baz( BTest bar ){}

void setHeading( Direction1 newDir ){}

void setup(){}

void loop(){}

int main(void)
{
      init();

      setup();
    
      for (;;)
            loop();
        
      return 0;
}

Now even the types.h include is below the forward declarations and neither of the enums work!

1 Like

Very nice analysis.

This reinforces my mistrust of "excessive magic" :slight_smile:

Thanks, and I agree about excessive magic. However, having spent plenty of hours with Atmel's AVR Studio and WinAVR, I do very much appreciate the simplicity that Arduino brings.