C++ Classes (issue)

Hey guys so I've been trying to gain some understanding of classes by creating a OOP program in one file that creates a player with custom or prior attributes. Then allows them to customize themselves and see what abilities they have.

The Issue:
1
2
In function 'int main()':
82:15: error: request for member 'toString' in 'newPlayer', which is of non-class type 'Player()'


I am getting this error when I attempt to create the Object then use any of its functions. When I remove the line newPlayer.toString(); and leave Player newPlayer(); I don't see my cout<<"Costructed player" from the constructor.

Source:
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
 
#include <iostream> 
#include <string> 

class Player
{
    public:     
    /* getters */ 
        std::string getID();
        int getLevel(); 
        double getExperience(); 
        void getAbilities(); 
    /* setters */    
        void setID(std::string); 
        void setLevel(int); 
        void setExperience(double); 
        
        void toString(); // simply print all the players info 
        
        Player(void); 
        Player(std::string, int, double); 
        
    private: 
        std::string playerID; 
        int playerLevel; 
        double playerExperience; 
        std::string playerAbilities[5] = {"Fight", "Defend", "Talk", "Walk", "Grab" }; 
};

/* 
    Player(void) - Constructor used to make new guest players that
    have the ability to change their name later. 
 */ 
Player::Player(void) 
{
    std::cout << "Guest player object constructed\n"; 
    setID("Guest"); 
    setLevel(0); 
    setExperience(0); 
} 
/*
    Player(string, int, double) - Constructor used to make custom players 
*/ 
Player::Player(std::string newID, int newLevel, double newExperience)
{
    std::cout << "Player object constructed\n"; 
    setID(newID); 
    setLevel(newLevel); 
    setExperience(newExperience);
}

/* getters */ 
std::string Player::getID() { return playerID; } 
int Player::getLevel() { return playerLevel; } 
double Player::getExperience() { return playerExperience; } 

/* setters */ 
void Player::setID(std::string newID) { playerID = newID; } 
void Player::setLevel(int newLevel) { playerLevel = newLevel; } 
void Player::setExperience(double newExperience) { playerExperience = newExperience; } 

// toString() - prints to the console the players name and attributes 
void Player::toString()
{
    std::cout << "Player ID: " << getID() << '\n'
              << "Player Level: " << getLevel() << '\n' 
              << "Player Experience: " << getExperience() << std::endl; 
} 
// getAbilities() - print all the abilities from the array using a loop 
void Player::getAbilities()
{
    for(int i=0; i < 4;i++)
    {
        std::cout << playerAbilities[i] << '\n'; 
    } 
}

int main() 
{ 
    Player newPlayer();  
    newPlayer.toString(); 
        
    return 0; 
} 


I'm using cpp.sh (C++ Shell Website) as I am on a computer without a compiler or IDE and maybe thats the issue?

Thanks if anyone can help me understand why Im getting this error.
Last edited on
Hey guys sorry to bother you with my sillyness I made newPlayer (an obj type) a function type where it should be Player newPlayer; instead of Player newPlayer();
Yep... you've stumbled across what's sometimes known as the Most Vexing Parse. Although you intended line 80 to define a Player object using the default constructor, what it actually does is declare a function called newPlayer, that returns a Player object.

It's an easy mistake to make!
If I may give a piece of advice, you may want to look into overloading the extraction operator for ostream to work with your code, so you could concatenate cout with the Player class.
Topic archived. No new replies allowed.