Im trying to use an object that i declared in main in another class

Hey guys, im trying to use an object that i declared in main() and use it in another class, but im getting an undeclared variable error.
Player Class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class player
{
public:
    int playerhealth = 0;
    int health()
    {
        return playerhealth;
    }
    
    void generate()
    {
        playerhealth = randomGen(150, 200);
    }
};


Section of main that generates object "mitchell"
1
2
3
4
5
int main()
{
    player mitchell;
    mitchell.generate();
}


and finally class "battle" which tries to call player.health
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
class battle
{
public:
    class monster zombie;
    int monhealth = zombie.health(0);
    
    void start(char monster2[])
    {
        
        while ( monhealth > 0 && mitchell.health() > 0 )
        {
            space(1);
            cout << "You are ATTACKED by a " << monster2 << " with " << monhealth << " HP!" << endl;
            
            if (weapon == 0)
            {
                noWeapon();
            } else if (weapon == 1)
            {
                knife();
            } else if (weapon == 2)
            {
                sword();
            }else if (weapon == 3)
            {
                bow();
            }
        }
    }
}


This is the line is is giving the error
 
while ( monhealth > 0 && mitchell.health() > 0 )


Thanks for your help! If this is too confusing, i can post the entire code. Basically im trying to generate a random health value that is consistant within the player class, which i can then access and modify through an object, if that makes sense. Im very new to OOP. Thanks :D
battle::start is a function. It can refer to variables that are members of the class, in global scope, or passed to the function as arguments.

Whatever calls the 'start' should pass a player to it and 'start' should accept such argument.

You should initialize player::playerhealth in the constructor of class player.
Topic archived. No new replies allowed.