Class called in a class

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.

this is the cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
#include <list>
#include <ctime>
#include <random>
using namespace std;
#include "classcreator.h"


int main()
{

    default_random_engine gen(time(NULL));
    uniform_real_distribution<double> teamn(10,18);
    normal_distribution <double> weightn(155,40);  ///138,70
    normal_distribution <double> abilityn(100,15);


    team TEAM [20];
    int  numteam;
    int  idstarter = 1000;
    int  weight;
    int  ability;

    for(int i=1; i<6; i++)
            {

                numteam = teamn(gen);
                if ((numteam >= 10) && (numteam <= 18)){

                TEAM[i].setpl(numteam);

                TEAM[i].display(i);

                    for (int x=1; x<(numteam+1) ; x++){

                        wrestler w [numteam];
                        weight = weightn(gen);    ///normal distribution for number of players
                        ability = abilityn(gen);

                        w[x].setid(idstarter+x);

                        if ((weight >= 93) && (weight <= 285)){

                        w[x].setweight(weight);
                        w[x].setability(ability);
                        w[x].display();

                }else{
                x--;
                }
                }
                cout<<endl<<endl;
                idstarter=idstarter+1000;

                }else{
                i--;
                }
            }
        cout<<TEAM[1].w[1].ability; ///here I'm having the problem
    return 0;
}


Here my header file


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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 ().
Last edited on
@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.

Thanks
Last edited on
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.
1
2
cout<<TEAM[1].w[1].ability;
               ^^^ Not allowed

Last edited on
@AbstractionAnon

So there is no other way to access w when its private with set and get?
Last edited on
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:
1
2
wrestler & getwr()  // Return a reference
{ return w; }


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.





@AbstractionAnon

Sorry, could you make an example code for me please(like ability), I'm kind of lost now. I try to follow one of your approaches, but I can't get it.

Please, Thanks

Last edited on
Approach #2 is very straight forward. I gave you an example of that above.
1
2
3
void team::set_wrestler_ability (int a)
{  w.setability(a);
}

Then you can do:
 
  TEAM[i].set_wresterler_ability (newval);


In approach #3, you need to change the w's getter to return a reference as I pointed out before.
1
2
3
wrestler & getwr()  // Returns reference
( return w;
)


Then you can do:
 
TEAM[i].getwr().setability(newval);




Topic archived. No new replies allowed.