Class definition order in C ++

I have a problem here. I am trying to define several classes, some of which are Players, and some are Pawns owned by players. Based on Python, I'm used to the fact that you can easily access a Pawn player through Pawn, as well as access game pawns through Player. Correct me if I am wrong, but in C ++ this seems impossible.

I am currently defining Player first, and one of its m_Pawns data m_Pawns must be vector<Pawn> . I declare a data item, but I do not assign it any value. I also define a member function that is designed to bind the pawn vector to m_Pawns , but I don't call it anywhere near the constructor. Since I do not name the constructor for Pawn in the constructor for Player , it seems I should be fine.

Here is my Player class. The Board class is predefined, while the Pawn class is subsequently defined (the Pawn class contains pointers to the owner of the Player class, so switching it doesn't help).

 class Player { public: Player(sf::Color color, const string& name); sf::Color GetColor(); string GetName(); void CreatePawns(Board& board, int which); protected: vector<Pawn> m_Pawns; sf::Color m_Color; string m_Name; }; Player::Player(sf::Color color, const string& name): m_Color(color), m_Name(name) {} sf::Color Player::GetColor() { return m_Color; } string Player::GetName() { return m_Name; } void Player::CreatePawns(Board& board, int which) { switch(which) { case 1: for(int i = 0; i < 4; ++i) { m_Pawns.push_back(Pawn((*board).Cluster1[i], this*, m_Color)); } break; case 2: for(int i = 0; i < 4; ++i) { m_Pawns.push_back(Pawn((*board).Cluster2[i], this*, m_Color)); } break; case 3: for(int i = 0; i < 4; ++i) { m_Pawns.push_back(Pawn((*board).Cluster3[i], this*, m_Color)); } break; default: cout << "Invalid player ID!\n\n"; break; } } 
+4
source share
4 answers

If class Player first appears, and class Pawn later, you can declare a pointer or a link to a later class (here Pawn ). You cannot have objects of a later class , for example

 class Player { Pawn* p; // allowed Pawn& r; // allowed vector<Pawn*> p; // allowed vector<Pawn&> vr; // not allowed (reference are not copyable) vector<Pawn> o; // error ! }; class Pawn {}; 

It is impossible to overcome this situation, since in C ++ for non-template class must show the full definition for declaring objects.

The only way out is to reformat your code or use a pointer / link (with forward declaration).

+1
source

The Pawn class still needs to be defined, so a compiled one can create a vector. You can leave with saving links or pointers to pawn objects in your vector instead of values; vector for example. In this case, it is enough to forward the declaration of the Pawn class.

0
source

You can do something like this:

 class Pawn; class Player { } class Pawn { } 
0
source

You can switch it because you can redirect the Player declaration, and then Pawn can have a pointer to it.

You can take a pointer to an incomplete type. You cannot store values โ€‹โ€‹of this type.

0
source

All Articles