#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
usingnamespace 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."
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.