Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Circular includes

Status
Not open for further replies.

dimroed

Programmer
Jun 22, 2003
4
CA
I have two classes, Foo and FooBox. FooBox contains a vector that stores Foo objects, so the FooBox.h file looks something like:

Code:
#ifndef FOOBOX
#define FOOBOX
#include "Foo.h"
class FooBox {
 vector<Foo> foos;
 ...
};
#endif

and the Foo.h file looks like

Code:
#ifndef FOO
#define FOO
#include &quot;FooBox.h&quot;
class FooBox {
 FooBox* parent;
 ...
};
#endif

This code won't compile in Visual C++ 6.0, and I'm guessing it has something to do with these circular includes. If I put the #include lines outside the #ifndef blocks, I get an error message from the compiler about possible infinite recursion; otherwise, I get an error message about the Foo class being undefined in the FooBox.h file.

How is it possible to get this kind of code to compile?
 
--- foo.h ---

#ifndef FOO
#define FOO

// Forward declaration (put include in .cpp)
class FooBox;

class Foo{
FooBox* parent;
...
};
#endif

Unless you implement class method calls (ie use the classes) in the .h you only need to #include classinfo in the .h if
1) Class is inherited
2) Class is a member

Ie you dont need to include if you just point to (or refer to it). Keeping the #includes in .h to a minimum is also a GoodThing(tm).


Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top