I ran into a little problem while programming my little games. I have two classes, each including the other's header file, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
//CGame.h
#ifndef C_GAME_H
#define C_GAME_H
...
#include "CGameState.h"
class CGame
{
...
CGameState* currentGameState; //error: ISO C++ forbids declaration of 'CGameState' with no type
...
};
I really only require CGame.h within CGameState because one of its member functions is important, so my solution was to include the CGame.h header file on the CGameState.cpp source file. No errors occur like that.
However, since I don't like leaving questions unanswered, could anyone explain me what is the problem with the first approach?
Both headers can't include each other. Eventually, one of the inclusion guards takes effect and keeps one of the headers from being included.
You need to forward declare the other class in one of the headers.
No, the forward declaration was created to have symbols visible before the actual definition of them.
They can be in some cases the only solution to a problem