Reputation: 1308
I have just entered into linking hell with an establish project. I move a few things in my header files around and am now running into the fact some objects cannot see other objects despite me using #indef, #define and #endif on every header file.
I noticed that on one of my older files, I use a class called Region in a class called World.
Because the class World kept complaining about not being able to see the other class, I was able to get past it by simply include class Region; above it.
Is there any way to avoid stuff like this?
Thank you.
Upvotes: 0
Views: 197
Reputation: 2590
One way I avoid include/dependency issues is to try to minimally declare class/struct definitions where possible (pointers/references only, however, thanks to Aldo for pointing that out). Say you have this class:
class foo
{
bar* barObject;
};
Obviously bar needs to be defined at some point previously. Instead of including bar's header file, instead we can simply add the following line right before foo's definiton.
class bar;
This prevents the compiler from complaining that bar isn't defined, but also does not require an entire header to be included. (known as forward declaration)
I've found this minimises a lot of pesky include issues that can occur when there are circular dependencies and whatnot.
This may or may not help in your case, since it's very hard to diagnose your specific issue with the information you have provided, but it could provide a starting point.
More reading: http://www-subatech.in2p3.fr/~photons/subatech/soft/carnac/CPP-INC-1.shtml
Upvotes: 2