C++ code cant detect the class or the int inside the class.

closed account (LAfSLyTq)
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
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
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;

	int damageDealt;
	char Action;
	 
class Boar;
class Player;
void battlePhase();
class Player
{
public:

	int health;
	int exp;
	int level;
	int atk;
	Player (bool Player)
	{
		health = 20;
		level = 1;
		exp = 0;
		atk = 10;
	}	
	void battlePhase()
{
cout <<"You have run into a Boar!\nWhat do you want to do?\n1=Attack "<<endl;
cin >> Action;
if ( Action == '1' || Action == 'i' )
{
cout << endl << name << " attacked " << "enemy" << " for " << Player::atk<< endl;
damageDealt = Player::atk;
Boar::health = Boar::health - damageDealt;
if ( damageDealt < 0 )
{
damageDealt = 0;
cout<<"No damage!"<<endl;
}
if ( damageDealt == 0 )
{
cout <<"No damage!"<<endl;
}
if ( damageDealt > 0 )
{
cout << "enemy" << " took " << damageDealt << " damage!";
}
if(Boar::health == 0)
{
	cout<<"enemy has died!"<<endl;
	cout<<"you have gained "<<"50"<<" exp!"<<endl;
	system ("CLS");
	Player::exp += 50;
}
else
{
cout << "\nThat isn't an action! Try typing '1'.";
battlePhase();
}

class Boar
{
public:

	int health;
	int level;
	int atk;
	Boar(bool Boar)
	{
		health = 20;
		level = 1;
		atk = 10;
	}
};


the probelm is that everywhere that says Boar::health it has health underlined RED and says "Error: a nonstatic member reference must be relative to a specific object."
Last edited on
I suggest you go back an re-read beginning sections on variable declaration and re-read sections on how to use classes.
Player::atk;

You can't do this.

A class is an outline for a "thing". An 'object' is the actual thing. You can have multiple objects of the same class. There could be multiple Players, each with their own 'atk' value. The computer doesn't know which player's atk you are trying to get with this code.

To give another example... std::string is a class. The 'length()' function is one of its members. Each string will have its own length.

So...
1
2
3
4
5
6
string foo = "333";
string bar = "4444";

cout << foo.length(); // prints '3', the length of foo
cout << bar.length(); // prints '4', the length of bar
cout << string::length();  // ? makes no sense!  compiler error 


What you are trying to do is the same as that last line.

You need to have an object on which you can get these members.

1
2
3
Player player;  // now you have a single player object.

player.atk = 5;  // now you can access that player's attack. 
closed account (LAfSLyTq)
what do i change, you arent making sense.
You've been told what the problems are. It's up to you to fix them. We aren't going to do it for you.
closed account (LAfSLyTq)
i dont know how to, and i dont know where he got that string foo thing from either. i dont know what hes trying to tell me to do.
Topic archived. No new replies allowed.