Not Understanding My Error

Hello,
I am struggling to find out why the IDE says that getPlayerID (line 25) is undefined. I don't understand how it's undefined and was wondering if someone could point out my error.

.h 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
  #include <string>
#include <iostream>
#include "FrisbeeGolfer.h"

using namespace std;

int main()
{
	string nameOfPlayer;
	FrisbeeGolfer playerOne;

	cout << " Frisbee Golfer, please type in your first and last name: " << endl;
	getline(cin, nameOfPlayer);
	playerOne.setPlayerName(nameOfPlayer);

	cout << endl;
	playerOne.welcomeMessage();

	string playerOneID;
	//FrisbeeGolfer playerOne;

	cout << "Please enter your Player ID: " << endl;
	getline(cin, playerOneID);
	playerOne.setPlayerID(playerOneID);
	cout << "Your Player ID is " << getPlayerID() << "." << endl;



	cin.ignore();
	cin.get();
	return 0;
};


.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
#include <string>
using namespace std;

class FrisbeeGolfer
{
private:
	string playerName;
	string playerID;
//	int tosses;
//	int games;
//	double average;
//------------------------------------------------------------------------
public:
	void setPlayerName(string name)
	{
		playerName = name;
	}
	string getPlayerName() const
	{
		return playerName;
	}
	void welcomeMessage() const
	{
		cout << "Welcome, " << getPlayerName() << "." << endl;
	}

	void setPlayerID(string id)
	{
		playerID = id; 
	}
	string getPlayerID() const
	{
		return playerID;
	}
};

Last edited on
getPlayerID() is a method. You need to call it the same way you called setPlayerID(). playerOne.getPlayerId();
@PadawanProgrammer

Shouldn't you call the getPlayerId function through an object since that function is in a class?

Like: playerOne.getPlayerId()
Last edited on
Now I feel very silly I did not catch that mistake....it's so obvious. I think I need more sleep or more coffee....or both! Thank you everyone for your help :)
Alright so now I have a new problem with this code. I am struggling to figure out how to write a function that will allow the players to type in the number of games they have played. I have tried adding this to the code that's above:
.cpp file

1
2
3
4
int playerOneTosses;
	cout << playerOne.getPlayerName << "Please type in your total number of tosses: ";
	getline(cin, playerOneTosses);
	playerOne.getPlayerTosses(playerOneTosses);



.h file
1
2
3
4
5
6
7
8
9
10
11
12
13

private:

int playerTosses;
//---------------------------------------------------
public:

	int getPlayerTosses() const
	{
		return playerTosses;
	}



As I was typing it I felt that it was wrong and sure enough it is. Could someone guide me on what to do or direct me to a tutorial or resource that can help me figure this one out?
Last edited on
I'll try it in just a sec and then I'll let you know :)
I tried it and this is what the IDE (Visual Studio) gave me as an error message:


Error C3867

'FrisbeeGolfer::getPlayerName':

non-standard syntax; use '&' to create a pointer to member
Frisbeegolf d:\class folders\csci 102\s01\frisbeegolf\frisbeegolf\frisbeegolf.cp
.h
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
#include <string>
using namespace std;

class FrisbeeGolfer
{
private:
	string playerName;
	string playerID;
	int playerTosses;
//	int games;
//	double average;
//------------------------------------------------------------------------
public:
	void setPlayerName(string name)
	{
		playerName = name;
	}
	string getPlayerName() const
	{
		return playerName;
	}
	void welcomeMessage() const
	{
		cout << "Welcome, " << getPlayerName() << "." << endl;
	}

	void setPlayerID(string id)
	{
		playerID = id; 
	}
	string getPlayerID() const
	{
		return playerID;
	}

	int getPlayerTosses() const
	{
		return playerTosses;
	}




};



.cpp
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
#include <string>
#include <iostream>
#include "FrisbeeGolfer.h"

using namespace std;

int main()
{
	string nameOfPlayer;
	FrisbeeGolfer playerOne;

	cout << "- First Frisbee Golfer, please type in your first and last name: " << endl;
	getline(cin, nameOfPlayer);
	playerOne.setPlayerName(nameOfPlayer);

	cout << endl;
	playerOne.welcomeMessage();

	string playerOneID;
	
	cout << "\nPlease enter your Player ID: ";
	getline(cin, playerOneID);
	playerOne.setPlayerID(playerOneID);
	cout << "\nYour Player ID is " << playerOne.getPlayerID() << ".\n" << endl;
	cout << "***************************************************************************" << endl;
//-----------------------------------------------------------------------------------------------

	FrisbeeGolfer playerTwo;

	cout << "\n- Second Frisbee Golfer, please type in your first and last name: " << endl;
	getline(cin, nameOfPlayer);
	playerTwo.setPlayerName(nameOfPlayer);

	cout << endl;
	playerTwo.welcomeMessage();


	string playerTwoID;

	cout << "\nPlease enter your Player ID: ";
	getline(cin, playerTwoID);
	playerTwo.setPlayerID(playerTwoID);
	cout << "\nYour Player ID is " << playerTwo.getPlayerID() << ".\n" << endl;
//-----------------------------------------------------------------------------------------------
	int playerOneTosses;

	cout << playerOne.getPlayerName << "Please type in your total number of tosses: ";
	cin >> playerOneTosses;
	// playerOne.getPlayerTosses(playerOneTosses);



	cin.ignore();
	cin.get();
	return 0;
};
I also tried it with line 49 of the .cpp document not commented out and it still gave me an error.
Looking through your code I don't believe you ever actually set the player tosses. So you are trying to return something that hasn't been given a value.

P.S. Don't useusing namespace std; in a header file. It's not that good of a practice.
Last edited on
PadawanProgrammer,

The short answer to the problem with line 49 is it should be playerOne.setPlayerTosses(playerOneTosses); not get. As Too Explosive said: you never set the playerTosses variable in the class.

Hope that helps your problem,

Andy
Last edited on
Okay, I fixed it now so that I can set the playerTosses but I am getting a new error in my .h file. Here is the code:
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
#include <string>
// using namespace std;

class FrisbeeGolfer
{
private:
	string playerName;
	string playerID;
	int playerTosses;
//	int games;
//	double average;
//------------------------------------------------------------------------
public:
	void setPlayerName(string name)
	{
		playerName = name;
	}
	string getPlayerName() const
	{
		return playerName;
	}
	void welcomeMessage() const
	{
		cout << "Welcome, " << getPlayerName() << "." << endl;
	}

	void setPlayerID(string id)
	{
		playerID = id; 
	}
	string getPlayerID() const
	{
		return playerID;
	}

	int setPlayerTosses(int tosses) const
	{
		playerTosses = tosses; // IDE says "Error: expression playerTosses must be a modifiable value"....It should be modifiable since I want the user to determine what quantity playerTosses is.
//unless I'm misunderstanding the message.
	}




};
Last edited on
Simply remove theconst fromint setPlayerTosses(int tosses) const
Topic archived. No new replies allowed.