A non-static member reference must be made relative to a specific object

closed account (LAfSLyTq)
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 <iostream>
#include <Windows.h>

using namespace std;

class player;
void name(player);

int main(){

};

void name(player){
	cout<<"What is your name?"<<endl;
	cout<<"Name: ";
	cin>>player.name;  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Error
};

class player{
public:
	int hp, atk, maxhp, lvl, money;
	string name;

	player(){
		name;
		hp = 20;
		maxhp = 20;
		atk = 5;
		lvl = 1;
		money = 0;
	}
};
Last edited on
player is the name of the class. In the name function you have to give the player parameter a name and use that instead of the class name.
1
2
3
4
5
void name(player p){
	cout<<"What is your name?"<<endl;
	cout<<"Name: ";
	cin>>p.name;
};


On line 6 you forward declare the player class. That will allow you to have pointers and references to player objects. The name function takes player by value (not by reference) so you have to define the player class before you declare the name function. In other words, the forward declaration doesn't do anything good here.

If you changed so that the player is passed by reference it's enough to have the forward declaration before the declaration of the name function but you still need to put the definition of the player class before the definition of the name function because it is accessing the player's members.
closed account (LAfSLyTq)
ah yes, i tried doing that before and it gave me this.

call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type

error is on that same line
Try putting it in parentheses like
cin>>(p.name);
closed account (LAfSLyTq)
no operator matches these operands" >>";
Put the 'class player {' definition above main, and change named to void name(player p), as below:

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
class player{
public:
	int hp, atk, maxhp, lvl, money;
	string name;

	player(){
		name;
		hp = 20;
		maxhp = 20;
		atk = 5;
		lvl = 1;
		money = 0;
	}
};

void name(player p){
	cout<<"What is your name?"<<endl;
	cout<<"Name: ";
	cin>>p.name;  
};

int main(){

};
closed account (LAfSLyTq)
my code so far

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 <iostream>
#include <Windows.h>

using namespace std;

class player;
void name(player p);

class player{
public:
	int hp, atk, maxhp, lvl, money;
	string name;

	player(){
		name;
		hp = 20;
		maxhp = 20;
		atk = 5;
		lvl = 1;
		money = 0;
	}
};

void name(player p){
	cout<<"What is your name?"<<endl;
	cout<<"Name: ";
	cin>>(p.name);
};

int main(){

};


still having the error 2678 and the intellisense error above
Last edited on
Add #include <string> .
Remove these lines:
1
2
class player;
void name(player p);


Also in the constructor of player:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class player{
public:
	int hp, atk, maxhp, lvl, money;
	string name;

	player(){
		name;
		hp = 20;
		maxhp = 20;
		atk = 5;
		lvl = 1;
		money = 0;
	}
};


name; Does nothing. Remove that line.
Topic archived. No new replies allowed.