Confused about implementing a set and get to a private object.

Hello all!

Here is my header
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
#include <stdio.h>
#include <string>
enum PlayerGender :  unsigned int {MALE, FEMALE, UNKNOWN}; 


class Player
{
private:
    char* userName;
    unsigned int level;
    PlayerGender gender;
    Player *next; 
    
public:
    Player ( const char* passed_in_username ); // allocate
    ~Player();
    Player ( const Player& other ); // copy constructor
    Player ( Player&& other ); // move constructor
    Player& operator= ( const Player& other ); // copy assignment
    Player& operator= ( Player&& other ); // move assignment
    
        void setName ( char* passed_in_name );
        void setLevel(unsigned int passed_in_level);
        void setGender(PlayerGender passed_in_gender);
        void setNext(Player passed_in_next);

        char *getName() const;
        unsigned int getLevel() const;
        char* getGender() const;
        Player* getNext() const;
    
};


I am having issues implementing

1
2
        void setNext(Player passed_in_next);
        Player* getNext() const;


I am not sure what I am doing wrong.

Any advice would be helpful. Since I am trying to abstract the class as much as possible I am not sure what extra information I can add at this point.
If you would like more context or if you have a question please ask.

Last edited on
The type of your private object next is 'Player *', so a pointer to a Player object. However, your function setNext has a object of type 'Player' as argument. You probably want this to be a pointer as well, or maybe a reference (so, Player const&)

You should understand the difference between:

1
2
3
Player player; // player is a object of type player.
Player other_player = player; // other_player is a NEW object, and a copy of the other object player.
Player* player_pointer = &player; // A pointer to the Player object player. 


Lastly, there's also the possibility of passing function arguments as reference:
1
2
3
4
5
6
7
8
void setNext(Player const& player) {
this->next = &player;
}

// Called like this:
Player player;
Player another_player;
player.setNext(another_player);


However, I think you will probably want to understand normal pointers first. Your setNext function should probably have an argument of type Player*.

Good luck!

edit: Be careful with the last example: When another_player goes out of scope, the reference (next) will become invalid.
Last edited on
Hi,

You probably shouldn't have those trivial set functions at all, use an initialiser list instead. You may not need the get functions either (possibly).

Think about what a player can do, write down these action words (verbs), that should give clues as to what functions you need. These will form the interface for your class.

Can you use std::string instead of char * ? I mean, do you have a particular reason to need char arrays?

Should you be using <iostream> instead of <stdio.h>

:+)
Hello again,

You probably shouldn't have those trivial set functions at all, use an initializer list instead. You may not need the get functions either (possibly).


@TheIdeasMan: you might be correct. I was/am writing the class very defensively because I am using it in a Hash Table that chains into a linked list for a data structures class, with a some restrictions on the interface. In other words I wanted to stay away from tweaking this class as much as possible when I started to work on my hash table. Also we are using char * as part of the exercise, so that is why I went by the rule of 5 just to prevent as many run-time debugging issues as possible.
Thank you for pointing out I should remove <stdio.h> I am using xCode at it plugs that into the header of all c++ classes. :)

@BasV Thank you for your guidance. I am clear on the differences you pointed out, I was just programming too long by the time I wrote this question and I had no idea what I was looking a by the time I was stuck. Thanks again for your help it was very helpful and exactly what I needed to know and be told!

Thanks!! again! everyone!
Last edited on
Topic archived. No new replies allowed.