Why class declaration ordering still matters with

2019-09-05 15:25发布

问题:

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)

回答1:

To fully declare GameSession the compiler needs to figure out the size of all of its members. So since there is a Player member, it needs the full declaration of Player.

If that player member was instead a pointer or reference, then the compiler wouldn't need to full declaration of Player because the compiler already knows the size of a pointer or reference regardless of what type of class is points or refers to.

As far as I know there aren't any magic ways to work around the requirement of having the full declaration when using a member that's an instance of a class. So you have to choose which technique and corresponding requirements you prefer.