Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ compilation error enum "does not name a type"

Tags:

c++

types

enums

The following code:

foo.h

#include "bar.h"
class foo{ 
public:
   enum my_enum_type { ONE, TWO, THREE }; 
   foo(); 
   ~foo() {} 
};

foo.cpp

foo::foo()
{
   int i = bar::MY_DEFINE;
}

bar.h

#include "foo.h"
class bar{
public:
   static const int MY_DEFINE = 10;
   foo::my_enum_type var;
   bar() {};
   ~bar() {};
};

Makes g++ compiler complain about my_enum_type "does not name a type". Why ? All headers have multiple inclusion defines (not shown here for clarity).

Thanks

like image 275
Rémy DAVID Avatar asked Oct 27 '10 10:10

Rémy DAVID


1 Answers

You must remove the cyclic dependency so you need to consider foo.cpp and foo.h as different units for this purpose.

  • bar class definition must see foo::my_enum_type so probably bar.h including foo.h is a necessity.

  • foo class definition does not use any of bar, so foo.h does not need to include bar.h

  • foo.cpp does need to see bar for MY_DEFINE so foo.cpp should include bar.h. That would actually also bring in foo.h automatically but you may wish to include it anyway in foo.cpp, just in case you remove the dependency later.

Presumably your headers have multiple include guards.

like image 63
CashCow Avatar answered Sep 19 '22 15:09

CashCow