Why class declaration ordering still matters with prototyping?

梦想与她 提交于 2020-01-25 20:57:47

问题


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.



来源:https://stackoverflow.com/questions/29998118/why-class-declaration-ordering-still-matters-with-prototyping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!