Circular Header files problem
Hello,
I have a problem and I don't know if there is any solution to it. I have three classes: Obj1, Obj2 and Obj3. Obj1 includes Obj2 and Obj2 includes Obj3, and Obj3 includes Obj1. for example: //Obj1.h #include "Obj2.h" //Obj2.h #include "Obj3.h" //Obj3.h #include "Obj1.h" Whenever I compile, I get a lot of errors saying that Obj1 is not declared wherever I use Obj1. There are a circular dependency and I'm wondering is there any way i can solve this problem, without changing the structure of the program?? Thanks, Alaa |
use forward prototypes or include them in the same header/namespace (if this fits the design)
|
Two ways:
(1): Wrap each header file in an #ifndef, like so: myheader.h: Code:
#ifndef _MYHEADER_H_ The second way, which is sometimes needed, is to prototype the class, function, or datatype being used. Like so: Code:
|
Thank you guys, I do have this in the header fils. The way i solved the problem is by moving some of the include header to the ".cpp" file so I don't get the circular definitions and it works beautifully.. Thanks anyway.
|
You should still do the "#if !defined(_MY_HEADER_H)" ...
regardless of whether or not you got past the immediate problem, The_nerd's advice is good advice - you should definitely do this with all of your headers.
IMHO ... |
I already have those in the header file. That was not the problem. The problem was where I define it. I had to define it in the .cpp file instead.
Thanks, Alaa |
All times are GMT -5. The time now is 06:28 AM. |