Reputation: 4391
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
Upvotes: 5
Views: 37315
Reputation: 23916
Problems:
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.
#ifndef
macros or #pragma once
directivesbar
might be forward declared in foo.h, your example doesn't reveal neccesity of this though).If the situation can't be resolved with these recommendations, use PIMPL idiom.
In short - just remove #include "bar.h"
directive from foo.h
Upvotes: 6
Reputation: 31435
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.
Upvotes: 6
Reputation: 132994
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 :)
Upvotes: 2