-
February 26th, 2012, 03:24 PM
#1
[RESOLVED] One object accessing another's data?
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:
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";
}
}
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:
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'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?
I'm sure this is a fairly simple problem, but I can't find the solution for the life of me.
Many thanks,
-P
-
February 26th, 2012, 06:22 PM
#2
Re: One object accessing another's data?
Originally Posted by phummon
Basically, I've got one object which has to access private data in another object... and can't.
1) I see no data that's private. Everything you've posted is either public or protected.
2) If you're deriving classes from GameUnit, you need to have GameUnit have a virtual destructor.
3) I have no problem compiling this code on the Comeau C++ compiler. Maybe you should post a complete example of what you're attempting to compile instead of snippets.
Code:
#include <string>
#include <iostream>
#include <vector>
class GameUnit
{
public:
std::string GetName() {return Name;}
protected:
std::string Name;
};
class Player
{
public:
void ListUnits();
protected:
std::vector<GameUnit*> MyUnits;
};
using namespace std;
void Player::ListUnits()
{
for(unsigned int i=0; i<MyUnits.size(); i++)
cout<<MyUnits[i]->GetName()<<"\n";
}
This is a complete module, and there is no error in compiling it.
Regards,
Paul McKenzie
-
February 27th, 2012, 03:20 AM
#3
Re: One object accessing another's data?
Originally Posted by phummon
Why can't a Player call this function?
Maybe you have GameUnit on a separate include file and just forgot to include it.
-
February 29th, 2012, 10:43 AM
#4
Re: One object accessing another's data?
Thanks for the tips, the solution was to indeed double-check the include files and make sure they were in a logical order. I know that sounds simplistic, but sometimes you can only spot these problems when a second set of eyes verifies your code.
Many thanks!
-P
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|