Two classes that need each others include file.

I looking at a state manager Online that someone has kindly decided to share http://gamedevgeek.com/tutorials/managing-game-states-in-c/ and it gets to the point where the CGamestate class is added to the engine class. The writer then goes back into the CGameState class and adds as parameters to three of the functions in there. The parameters are of the CEngine type, but the CEngine class is using the the CGameState class to add a vector of CGameState pointers to the engine.

The engine wont compile without errors if CEngine and CGameState each have the others include headers. Is there a work around or am I doing something wrong?

Additional: In my classes I'm using #ifndef and #define. I've tried removing these from both class files and compiling but it errors. : fatal error C1014: too many include files : depth = 1024
If you only have pointers or references of the class you don't need the class to be defined. Instead of including the header (that defines the class) you can use a forward declaration of the class.

In the CGameState header you don't need to include the CGameEngine header. You can just write class CGameEngine; instead.

In the CGameState source file you probably need to include the CGameEngine header because you are actually using CGameEngine objects but that is no problem.

Same with the CGameEngine header. You just need class CGameState;
Last edited on
Thank you. I knew there would be a simple solution. Thanks again for the quick response.
Topic archived. No new replies allowed.