Issue with headers

Hey everyone,

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
        ...
};


1
2
3
4
5
6
7
8
9
10
11
12
//CGameState.h
#ifndef C_GAME_STATE_H
#define C_GAME_STATE_H

#include "CGame.h"

class CGameState
{
    ...
};

#endif 


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?

Best Regards,
~Deimos
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.
Is forward declaring to solve this problem bad programing practise?
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
Obligatory link:

http://cplusplus.com/forum/articles/10627/

See sections 4 and up. Specifically, what you're trying to do is covered by section 6.
Topic archived. No new replies allowed.