header file problems

Hi, I have a question. When I try to do this..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//--- socket.h
#include "player.h"
class Socket {
public:
...
private:
   Player* player;
};

//--- player.h
#include "socket.h"
class Player {
public:
...
private:
   Socket* sock;
};


... I get errors. Basically I am including player.h into socket.h in order to make a pointer to the Player class, however I am also including socket.h into player.h so that i can make a pointer to the Socket class. The problem is socket.h already includes player.h so I am effectively including player.h into player.h.. which is causing me compile problems.

So what would be the best way to do this? Basically I am trying to make an object of the Player class and Socket class to both have a pointer pointing at each other.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
//--- socket.h
#include "player.h"
class Socket {
private:
   Player* player;
};

//--- player.h
class Socket;
class Player {
private:
   Socket* sock;
};


you have to declare a class before you use it.
I hope this helps
Thank you very much for the reply, it helped a lot!
Topic archived. No new replies allowed.