Hi everyone,
I'm a moderate-level C++ programmer who is a little rusty at the moment. I've got an object question which is driving me nuts. I'm sure this is a C++ 101 level question, but for the life of me, I can't recall the solution. Basically, I've got one object which has to access private data in another object... and can't.
Here's the specifics: I'm writing a little war game program where players deploy units (soldiers, tanks, planes, etc.) onto a gameboard. Players and Units are modeled as objects:
Here's the problem: In the above code, Player's ListUnits() function doesn't work because Player can't access GameUnit's GetName() function. Specifically, here's the compiler's error message:Code:class GameUnit { public: string GetName() {return Name;} protected: string Name; }; class Player { public: void ListUnits(); protected: vector<GameUnit*> MyUnits; }; void Player::ListUnits() { for(unsigned int i=0; i<MyUnits.size(); i++) { cout<<MyUnits[i]->GetName()<<"\n"; } }
I've tested enough to realize that the problem is the GameUnit::GetName() function is a public function within the GameUnit object. Why can't a Player call this function? Making both friend classes of each other doesn't help. Do I have to make these guys related somehow?Code:In file included from Main.cpp:18: Player.h: In member function 'void Player::ListUnits()': Player.h:47: error: 'GetName' undeclared (first use this function) Player.h:47: error: (Each undeclared identifier is reported only once for each function it appears in.)
I'm sure this is a fairly simple problem, but I can't find the solution for the life of me.
Many thanks,
-P


Reply With Quote

Bookmarks