ADT class not compiling

Hello Guys

Thank you for helping me, my ADT class is not compiling , how do i fix it ?

regards

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
  
//Define a class Player as an ADT that uses separate files for the interface and the implementation.
//The class represents a Player in a video game. This class has the following data members:

//string name; // the name of the player
//string team; // the team for which this player plays
//int level; // the level to which the player has advanced to in the game.
//int points; // the number of points accumulated

//The class should contain a default constructor that initializes name to "Player 0", team to “Team 0”, level to 0 and points to 0.
//It should also contain an overloaded constructor that accepts four parameters to set the name, team, level and points to specified values.
//The destructor should output "Game Over".

//Include accessor functions that return the values stored in each of the member variables of an object of class Player
//(i.e. get functions), as well as mutator functions to update each of the member variables of an object of class Player respectively
//(i.e. set functions with a parameter to set each member variable to a value specified by the parameter).

//The class should also contain a void member function called reset() that resets the member variables of a Player to values specified by parameters.

//Overload the equality operator == as a friend function for class Player.
//This function returns true if the team member variable of Player1 is identical to that of Player2 and false otherwise.
//Use the following prototype:
//bool operator==(const Player & Player1, const Player & Player2)

//Overload the comparison operator> as a friend function for class Player.
//This function returns true if the points member variable of Player1 is bigger than that of Player2 and false otherwise.

//Use the following prototype:
//bool operator>(const Player & Player1, const Player & Player2)
//Define an overloaded prefix operator ++ (implemented as a friend function)
//to return the current instance of the class Player after incrementing the points by 1.

//For every 100 points scored, the level is also incremented by 1. Hint: Use the % (modulus) operator.
//A player can request hints while playing the game.
//For every hint requested, 10 points should be deducted from a player’s points, and the level
// (if necessary) at which (s)he plays adapted accordingly.
// To implement this, define a void member function requestHint()to print a message “Please give me a hint”
// (We will not try to supply the hints themselves at this stage or worry about how this will be done.)

//Member function requestHint()should then adapt the points and level by calling the overloaded prefix operator-- to return
//the current instance of the class Player after decrementing the points by 10 and adapting the level accordingly.
//You should also implement the overloaded prefix operator-- as a friend function.

//Test your class by writing a program to do the following:
//• Overload the stream extraction operator >> and the stream insertion operator << as friend functions for class Player.
//    The stream insertion operator << should display the name, team, level and points of a player.
//• Use the default constructor to instantiate two objects player1 and player3.
//• Use the overloaded constructor to instantiate an object player2 with the following values for the member variables:

//Name: “Jane”
//Team: “BlueSwallows”
//Level: 1
//Points: 99

#include <iostream>
#include <string>
using namespace std;
// Header file  VPLAYER.H : This is the interface for the class player.
// Values for this type are player1, player2, and player 3. The member variables are name, team , score and level
//
#ifndef VPLAYER_H
#define VPLAYER_H

class Player
{
    private :
    string name ;   // name of the Player
    string team ;   // Team which player plays for
    int level ;     // the level to which the player has advanced to in the game
    int points;     // the number of points accumulated

    Player();

    Player( string &name, string &team, int &level, int &points)
    {
        name = " Player 0 ";
        team = " Team 0 ";
        points =  0;
        level =   0;
    }

 friend void getPlayerName(string &name)
    {
        cout<<"Enter Player Name : "<<endl;
        getline(cin,name);
        cout<<endl;
    }

friend void getTeamName(string &team)
    {
        cout<<" Enter Team Name : "<<endl;
        getline(cin,team);
        cout<<endl;
    }

    void resetPlayer(string &name, string &team, int &level, int &points )

    {
        name = " Player 1 ";
        team = " Blue Swallow ";
        level =   2;
        points =  99;

    }

    friend bool operator == ( const Player &player1, const Player &player2 )
    {
        if ( player1 == player2 )
        {
          //  cout<<" Player 1 : "<<player1<<" is the same as Player 2:"<<player2<<endl;
          //  cout<<endl;

        }
        else
        {
          //  cout<<" Player 1 : "<<player1<<" is not the same as Player 2:"<<player2<<endl;
          //  cout<<endl;
        }

    }

    friend bool operator > (const Player & player1, const Player & player2)
    {
        return ( player1.name == player2.name);

    }

    friend bool operator -( const Player & player1, const Player & player2 )
    {
        player1.level-1;
        player1.points-10;
        cout<<" You're on Level "<<player1.level<<" with "<<player1.points<<" points "<<endl;
        cout<<endl;
        return (( player1.level)&& (player2.level));

    }

    friend int getPoints (string &name, string &team, int &points )
    {
        cout<<" Get player points "<<endl;
        cin>>points ;
        cout<<'\t'<<name<<" has "<<points<<" points "<<endl;
        cout<<endl;
        return points;
    }

    friend int getLevel( string &name, string &team, int &points  )
    {
        if (points >= 100)
        {
            points %= 100;
            cout<<" You are on level :"<<points<<endl;
            cout<<endl;

        }
        else
        {
            cout<<" You are on level : 0 " <<endl;
            cout<<endl;

        }

        return points;
    }


    friend void getPoints ( int &points, int &level, string &name, string &team)
    {
        if (points >= 100)
        {
            points %= 100;

            if (points >= 1)
            {
                level++;
                cout<<" You're playing on level : "<<level<<endl;
            }
        }
    }

    friend void requestHint ( int &points, int &level, string &name, string &team)
    {
        points -= 10 ;
        if (points <= 100)
        {
            --level;
            cout<<" You're now on level : "<<level<<endl;
            cout<<endl;

        }
    }

    friend void displayPlayerDetails( string &name, string &team, int &level, int &points)
    {
        cout<<" Name : "<<name<<endl;
        cout<<" Team : "<<team<<endl;
        cout<<" Level : "<<level<<endl;
        cout<<" Points : "<<points<<endl;
    }

    ~Player()
    {
        cout<<" Game Over "<<endl;
    }
};

#endif // VPLAYER_H

int main()
{

 Player p;

 string playerName = "", playerTeam = " ";
 char hint;
 int playerlevel = 0, playerPoints = 0;


 p.Player();
 p.getPlayerName(playerName);
 p.getTeamName(playerTeam);
 p.getPoints( playerName,playerTeam);
 p.getLevel( playerName,playerTeam, playerPoints);

 cout<<" Do you need a hint ?  Press Y or N "<<endl;

 if ( hint == 'y'|| hint == 'Y')
            {
             cout<<" You have requested for a hint "<<endl;
             cout<<" You will forfeit 10 points and go one leverl lower "<<endl;
             p.requestHint( playerPoints, playerlevel, playerName, playerTeam );
            }
                if (hint == 'n'|| hint == 'N' )
                        {
                            cout<<" Play continues "<<endl;
                            cout<<endl;
                        }
                            else
                            {
                                cout<<" Enter a valid option Y or N "<<endl;
                            }

    p.displayPlayerDetails(playerName, playerTeam, playerlevel, playerPoints);

    return 0;
}

 
Please post the compiler errors.
how do i fix it

try reading the compiler error messages and addressing them, all of them are quite basic like trying to access private methods outside scope, class ... has no member named ... etc
Last edited on
hello

herewith the compilier errors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
In function 'bool operator==(const Player&, const Player&)':
120:5: warning: no return statement in function returning non-void [-Wreturn-type]
 In function 'bool operator-(const Player&, const Player&)':
130:22: warning: statement has no effect [-Wunused-value]
131:23: warning: statement has no effect [-Wunused-value]
 In function 'int main()':
72:5: error: 'Player::Player()' is private
212:9: error: within this context
201:5: error: 'Player::~Player()' is private
212:9: error: within this context
219:4: error: invalid use of 'Player::Player'
220:4: error: 'class Player' has no member named 'getPlayerName'
221:4: error: 'class Player' has no member named 'getTeamName'
222:4: error: 'class Player' has no member named 'getPoints'
223:4: error: 'class Player' has no member named 'getLevel'
231:16: error: 'class Player' has no member named 'requestHint'
243:7: error: 'class Player' has no member named 'displayPlayerDetails'
Most of those error messages seem quite self-explanatory.

For the last several errors you seem to be overusing the "friend" concept. Remember a friend is not really part of your class, it is some external function that you're giving access to your privates, and you really only want close friends to have access to your privates.

how do i fix line 7 and 9 of the error log, if i get these right that then it will work, can you help ? The question above requires me to name these functions as friends as this is an ADT
Last edited on
Why do you have the constructor and destructor private, instead of the normal public?

The question above requires me to name these functions as friends as this is an ADT

Please provide me the definition for ADT that you're referring to.

Where does this:

//Include accessor functions that return the values stored in each of the member variables of an object of class Player
//(i.e. get functions), as well as mutator functions to update each of the member variables of an object of class Player respectively
//(i.e. set functions with a parameter to set each member variable to a value specified by the parameter).


Tell you that the getter and setter functions should be friends? The getter and setter functions need to be part of the class not friends of the class. If you have getter and setter functions for every variable in the class then you don't even need friend functions, just call the public getter and setter functions.

Also look at this function: friend int getPoints (string &name, string &team, int &points)

Since you're passing values into this function there is no reason that this function needs to be either a friend or even a member function. This function should probably look more like: int getPoints();
Note the lack of parameters and the lack of the friend qualifier, you should return the value held in your class member variable points. Remember that class member functions have full access to all class member variables and functions.



Last edited on
thank you for your prompt response, I need to find out form you what to do in order for this program to run, i would be very grateful if you could point me what to to next and solve ,

The ADT i mean is Abstract Data Type. In my reading i understood that to make a class ADT you must declare everything as private.

Thank you for your patience. i look forward to you help. Thank you .

Last edited on
The ADT i mean is Abstract Data Type. In my reading i understood that to make a class ADT you must declare everything as private.

No, not everything must be private. Class member functions are usually public, and if you want to be able to use your constructors and destructors outside of the class they must also be public.

So to fix the program you need to make sure all of your member functions are in the public: section of the class and most of them should not be friends.

Since you're supposed to be breaking up the definition and the implementation your definition should start with something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Player
{
  public:
    Player() = default;  // Use the compiler supplied default constructor.

    Player( string &name, string &team, int &level, int &points);
    
    // Setter functions:
    void setPlayerName(const string &name);
    void setTeamName(const string &team);

    // Getter functions:
    std::string getPlayerName();
    std::string getTeamName();
  private :
      string name ;   // name of the Player
      string team ;   // Team which player plays for
      int level ;     // the level to which the player has advanced to in the game
      int points;     // the number of points accumulated

};


The function implementations should go into a source file (.cpp). This is only showing a couple of the functions but the others will be similar.



Jib wrote:
you really only want close friends to have access to your privates.


Indeed ( ͡° ͜ʖ ͡°)
Last edited on
Topic archived. No new replies allowed.