What can I use instead of #include if I want all my units to be able to use each other... cuz now it displays "Include files nested too deep" error :S
thx!
Printable View
What can I use instead of #include if I want all my units to be able to use each other... cuz now it displays "Include files nested too deep" error :S
thx!
Max include nesting level is compiler specific. One thing you can do is make sure you have a protection on your header files so they can only be included once. Either one will work fine:
Code:// works on VC++ and some other compilers
#pragma once
// ... your header source here ...
Code:// will work anywhere. just make sure to replace the name of the header file
#ifndef _NAMEOFHEADER_H
#define _NAMEOFHEADER_H
// ... your header source here ...
#endif
Also, make your own headers #include as few other headers as possible. If you don't need a complete type, don't include the header, just forward declare the classes.
For example, this would be unnecessary:
Here, you will notice that in the above code, it was NOT necessary for B to be a complete type. B is only used as a reference (B&), a pointer (B*), or a parameter/return type, so it can legally still be an incomplete type (a type that has been declared but not defined). There are no member variables of type B (only type B& or B*), and B is not a base class of A. Further, we have no containers of B that might need to know its definition. So instead of defining B by the include, we declare it instead:Code:A.H
#ifndef __A_H_INCLUDED_____
#define __A_H_INCLUDED_____
#include "b.h" // assume this defines class B
class A{
private:
B& myBObject;
B* myBPtr;
public:
explicit A(B&);
void otherFunction(B*);
B f2();
void f3(B);
};
#endif
In the code above, I also show where A is declared and defined. Note A can be declared as often as you like; it should only be defined in one header. Learn all the rules about incomplete types; often only a declaration is needed, not a definition.Code:A.H
#ifndef __A_H_INCLUDED_____
#define __A_H_INCLUDED_____
class B; //declares class B
class A/*A is declared here*/{
private:
B& myBObject;
B* myBPtr;
public:
explicit A(B&);
void otherFunction(B*);
B f2();
void f3(B);
}/*A is defined here*/;
#endif
Now, A.CPP will certainly need to include B.H (A.CPP will need B to be a complete type, because it will want to use member functions or data of B, and it must know how to copy B objects). But the goal is to make your headers (.h) files include as few other headers as possible; put your includes in the .CPP files only when possible.
Never #include another header of yours unless you actually need a complete type. For an incomplete type, just forward declare it.
You can also use tricks like the PIMPL idiom to hide your class's "private parts", which will probably cut down drastically on the need for full definitions of classes.