I was struggling with a 'field "player" has incomplete type" error with my code. My class "game_session" contains a single "player" and I declare them both in the same header file as shown below:
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
#include <vector>
using std::vector;
class Player;
class GameSession;
class GameSession{
private:
...
Player player;
public:
GameSession();
~GameSession();
...
};
class Player {
public:
Player( int maxdim );
~Player();
...
};
The above code would not compile because the GameSession could not find a declaration for the Player class. It worked when I switched the two classes as shown below:
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
#include <vector>
using std::vector;
class Player {
public:
Player( int maxdim );
~Player();
...
};
class GameSession{
private:
...
Player player;
public:
GameSession();
~GameSession();
...
};
I no longer needed the prototypes. My question is why did the prototyping not prevent the errors from a lack of ordered declaration? Also, how can this be avoided in the future for when there are many classes and dependencies?
Danke
(For those wondering, I used an initializer list in the class implementation to deal with the fact Player does not have a default constructor)