开发者

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

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 "doe开发者_开发问答s not name a type". Why ? All headers have multiple inclusion defines (not shown here for clarity).

Thanks


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.


Problems:

  • no multiple inclusion protections
  • cyclic inclusion
  • type use before its declaration caused by cyclic inclusion

Your foo.h being processed by C preprocessor looks like infinite empty string sequence.

With multiple inclusion protection foo.h is preprocessed to:

> cpp foo.h
class bar{ // preprocessed from #include "bar.h"
public:
   static const int MY_DEFINE = 10;
   foo::my_enum_type var;
   bar() {};
   ~bar() {};
};
// end of #include "bar.h"
class foo{ 
public:
   enum my_enum_type { ONE, TWO, THREE }; 
   foo(); 
   ~foo() {} 
};

This obviously is not a valid C++ code - foo is used in bar body without previous declaration. C++, unlike Java, requires types to be declared before use.

  • Use multiple inclusion protection. Depending on your platform those might be #ifndef macros or #pragma once directives
  • Remove bar.h inclusion from foo.h.
  • Place forward declarations where needed (in your case bar might be forward declared in foo.h, your example doesn't reveal neccesity of this though).
  • Move as much implementation as possible to *.cpp files.

If the situation can't be resolved with these recommendations, use PIMPL idiom.

In short - just remove #include "bar.h" directive from foo.h


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

should be

foo::foo()
{
   //...
}

Also note that

static const int MY_DEFINE = 10;

is still a declaration, although it has an initializer. In bar.cpp you should have the following line

const int bar::MY_DEFINE;

Also you can't include bar.h from foo.h and foo.h from bar.h... that's physically impossible :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜