Hi, I'm teaching myself c++, and I'm stuck on this problem. I have a class wrestler and a class team. The class wrestler as you can see has ability point and weight and etc...
The class team has wrestler and points.
Here my code, this program creates 20 different teams with a random number of wrestler with a random ability and random weight.
class wrestler{
public:
wrestler(){}
~wrestler(){}
void setid(int i){id = i;}
int getid(){return id;}
void setweight(int w){weight = w;}
int getweight(){return weight;}
void setability(int a){ability = a;}
int getability(){return ability;}
void display(){cout<<"Player number "<<id<<" weights "<<weight<<" and it has an ability score of "<<ability<<endl;}
private:
int id;
int weight;
int ability;
int weightclass;
int wins;
int losses;
};
class team{
public:
team (){}
~team (){}
void setpl(int p){players = p;}
int getpl(){return players;}
void setp(int o){points = o;}
int getp(){return points;}
void setwr(wrestler t){w = t;} ///I guess that I'm having here the problem
wrestler getwr()(return w;)
void display(int g){cout<<"There are "<<players<<" players in the Team number "<<g<<endl<<endl;}
private:
int points;
int players;
wrestler w;
};
I can't understand the problem I'm having I need help, thanks.
I can't understand the problem I'm having I need help, thanks.
We can't help if you don't tell us what problem you're having.
Some comments:
main.cpp
Line 37: A variable dimension is not standard C++. C++ standard requires than array dimensions be known at compile time. Some compilers do allow this as a non-standard extension.
Line 60: team::w is not an array. You can't subscript it. It's also a private member. You can't access it directly.
Shouldn't a team have multiple players? I suggest you use a std::vector for wrestlers and teams.
header.h:
line 41: Should be {} around the return statement, not ().
@AbstractionAnon
Sorry it's my first post.
I think the main question is How do I access a class that is a private member of another class?
The line ( cout<<TEAM[1].w[1].ability; ///here I'm having the problem ) is it just a test, i want to see if I can access this variable given that I have a few function where I have to compare the ability score of two different wrestlers from two differen teams.
The short answer is you can't because w is private. private says that you don't want to allow access to the member variable from outside the class.
You have a few choices:
1) Make wrestler public within team. Not the best choice. Doing so breaks encapsulation.
2) Write a public function inside team such as set_wrestler_ability (int val).
That function could then access w. Since ability is also private, you would then have to call setability().
1 2 3
void team::set_wrestler_ability (int a)
{ w.setability(a);
}
3) have getwr() return a reference. You could then do:
TEAM[i].getwr().setability(newval);
Note, as I pointed out before, w is not subscripted. You can't index it.
You can't have it both ways. If you want w private, then you're restricting access to it.
Approach #2 is what I call a second level setter. i.e. it is a setter which calls a setter of a private member. I'm not fond of second level setters, but sometimes you need them.
My preference is approach #3. Line 41 in your header would change as follows:
I think you're still missing the concept of a team having multiple wrestlers. i.e. w should be an array or vector. The fact that you have team::players implies you can have multiple wrestlers, but w is not an array or vector. As I suggested earlier, you should use a std::vector for w.
There's also another problem at lines 37-47 (main). You create an array of wrestlers, but that array goes out of scope at line 48.