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; } }
source share