Using variable from one class in another

I have a variable called "health" thats in a class called "player" and i need to use it in a class called "dinosaur" but i cant and i have never had to do this before and cant figure it out, can you please help.

player class:

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#ifndef PLAYER_H
#define PLAYER_H

#include <vector>
#include <string>

class player
{
    private:
        int health;
        int pistolAmmo;
        int shotgunAmmo;
        int rifleAmmo;
        int score;
        int money;
        int healthPacks;
        int items[];
        //std::vector<std::string> inventory;

    public:
        void save();
        void load();
        void MainGame();
        void store();
        void timer();
        void backpack();
        void stats();
        player();
        ~player();

        void set_health(int H);
        void set_pistolAmmo(int PA);
        void set_shotgunAmmo(int SA);
        void set_rifleAmmo(int RA);
        void set_score(int S);
        void set_money(int M);
        void set_items(int I);
        void set_healthPacks(int HP);
        //void set_inventory(std::vector<std::string> I);

        int get_health();
        int get_pistolAmmo();
        int get_shotgunAmmo();
        int get_rifleAmmo();
        int get_score();
        int get_money();
        int get_items();
        int get_healthPacks();
        //std::vector<std::string> get_inventory();
};

player::player()
{
    health = 100;
    pistolAmmo = 0;
    shotgunAmmo = 0;
    rifleAmmo = 0;
    score = 0;
    money = 600;
    healthPacks = 0;
    items[4];
}

player::~player()
{
    //Empty Deconstructor
}

void player::set_health(int H)
{
    health = H;
}

void player::set_pistolAmmo(int PA)
{
    pistolAmmo = PA;
}

void player::set_shotgunAmmo(int SA)
{
    shotgunAmmo = SA;
}

void player::set_rifleAmmo(int RA)
{
    rifleAmmo = RA;
}

void player::set_score(int S)
{
    score = S;
}

void player::set_money(int M)
{
    money = M;
}

void player::set_items(int I)
{
    items[4] = I;
}

void player::set_healthPacks(int HP)
{
    healthPacks = HP;
}



int player::get_health()
{
    return health;
}

int player::get_pistolAmmo()
{
    return pistolAmmo;
}

int player::get_shotgunAmmo()
{
    return shotgunAmmo;
}

int player::get_rifleAmmo()
{
    return rifleAmmo;
}

int player::get_score()
{
    return score;
}

int player::get_money()
{
    return money;
}

int player::get_items()
{
    return items[4];
}

int player::get_healthPacks()
{
    return healthPacks;
}

#endif // PLAYER_H




dinosaur class:

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#ifndef DINOSAUR_H_INCLUDED
#define DINOSAUR_H_INCLUDED

#include <iostream>
#include <ctime>
#include <random>
#include "player.h"

using namespace std;

class dinosaur
{
    private:
        int dinosaurHealth;
        int attacks[];

    public:
        void trex();
        void velociraptor();
        dinosaur();
        ~dinosaur();

        void set_dinosaurHealth(int DH);
        void set_attacks(int A);

        int get_dinosaurHealth();
        int get_attacks();

};

dinosaur::dinosaur()
{
    dinosaurHealth = 100;
    attacks[0];
}

dinosaur::~dinosaur()
{

}

void dinosaur::set_dinosaurHealth(int DH)
{
    dinosaurHealth = DH;
}

void dinosaur::set_attacks(int A)
{
    attacks[0] = A;
}

//--------------------------------------------

int dinosaur::get_dinosaurHealth()
{
    return dinosaurHealth;
}

int dinosaur::get_attacks()
{
    return attacks[0];
}


void dinosaur::trex()
{
    int TrexHealth = 0;
    int attacks[5] = {5,9,13,19,23};
    time_t T;
    time(&T);
    srand(T);
    int Time;

    player P;

    Time = rand() % 5;

    TrexHealth = dinosaurHealth;

    switch(Time)
    {
        case 0:
            {
                cout << "T-Rex used Bite!!" << endl;
                health -= 5;
            }break;
    }


}

void dinosaur::velociraptor()
{

}

#endif // DINOSAUR_H_INCLUDED
Pass the Player to be bitten to your trex function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// this dinosaur (acting as a trex) bites the player P
void dinosaur::trex(player& P)// by reference so it can be changed
{
    int TrexHealth = 0;
    int attacks[5] = {5,9,13,19,23};
    time_t T;
    time(&T);
    srand(T);
    int Time;

    Time = rand() % 5;

    TrexHealth = dinosaurHealth;

    switch(Time)
    {
        case 0:
            {
                cout << "T-Rex used Bite!!" << endl;
                P.health -= 5;// P's health decreased
            }break;
    }
}
Last edited on
I get these errors

C:\Users\Chay\Desktop\Dinosaur Arena\player.h||In constructor 'player::player()':|
C:\Users\Chay\Desktop\Dinosaur Arena\player.h|61|warning: statement has no effect|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h||In constructor 'dinosaur::dinosaur()':|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|34|warning: statement has no effect|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|65|error: prototype for 'void dinosaur::trex(player&)' does not match any in class 'dinosaur'|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|18|error: candidate is: void dinosaur::trex()|
||=== Build finished: 2 errors, 2 warnings ===|
Both errors are because the prototype must match the definition.
Line 18 in dinosaur.h therefore should be: void trex(Player&);
ok i changed it and am still getting errors, im not sure what they mean

C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|18|error: 'player' has not been declared|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h||In constructor 'dinosaur::dinosaur()':|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|34|warning: statement has no effect|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|65|error: variable or field 'trex' declared void|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|65|error: 'player' was not declared in this scope|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|65|error: 'P' was not declared in this scope|
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\winnt.h|2422|error: expected declaration before end of line|
||=== Build finished: 5 errors, 1 warnings ===|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|18|error: 'player' has not been declared|
means you typed void trex(player&); instead of
void trex(Player&); and the rest of the errors follow from this.
Ok i tried that too and it still doesnt work. My other class is just "player" its not capitalized.
Oops. My bad. It should be player then.
I don't know what the problem is then. You have #include "player.h" at the top of dinosaur.h, so the compiler should recognize a player object.
The errors read like it doesn't know what a player is.
New code?

EDIT: You had no problem with line 74 player P; before right?
Weird.
Last edited on
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#ifndef DINOSAUR_H_INCLUDED
#define DINOSAUR_H_INCLUDED

#include <iostream>
#include <ctime>
#include <random>
#include "player.h"

using namespace std;

class dinosaur
{
    private:
        int dinosaurHealth;
        int attacks[];

    public:
        void trex(player&);
        void velociraptor();
        dinosaur();
        ~dinosaur();

        void set_dinosaurHealth(int DH);
        void set_attacks(int A);

        int get_dinosaurHealth();
        int get_attacks();

};

dinosaur::dinosaur()
{
    dinosaurHealth = 100;
    attacks[0];
}

dinosaur::~dinosaur()
{

}

void dinosaur::set_dinosaurHealth(int DH)
{
    dinosaurHealth = DH;
}

void dinosaur::set_attacks(int A)
{
    attacks[0] = A;
}

//--------------------------------------------

int dinosaur::get_dinosaurHealth()
{
    return dinosaurHealth;
}

int dinosaur::get_attacks()
{
    return attacks[0];
}


void dinosaur::trex(player &P)
{
    int TrexHealth = 0;
    int attacks[5] = {5,9,13,19,23};
    time_t T;
    time(&T);
    srand(T);
    int Time;

    Time = rand() % 6;

    TrexHealth = dinosaurHealth;

    switch(Time)
    {
        case 0:
            {
                cout << "T-Rex used Bite!!" << endl;
                P.health -= 5;
            }break;
        case 1:
            {
                cout << "T-Rex used Stomp!!" << endl;
                P.health -= 9;
            }break;
        case 2:
            {
                cout << "T-Rex used Crunch" << endl;
                P.health -= 13;
            }break;
        case 3:
            {
                cout << "T-Rex used Slam" << endl;
                P.health -= 19;
            }break;
        case 4:
            {
                cout << "T-Rex used Pulverize" << endl;
                P.health -= 23;
            }break;
        default:
            {
                cout << "T-Rex attack missed!!" << endl;
                P.health -= 0;
            }
    }
}

void dinosaur::velociraptor()
{

}

#endif // DINOSAUR_H_INCLUDED
Sorry, I don't see anything wrong.
Try void trex(player &P); for line 18 (ie. including the P).
I don't think that's necessary though, so it's a long shot.

Otherwise, I just don't see anything wrong.
I guess you will just have to ignore my advice and go back to what you had.
Sorry for trashing your thread.
Hmm yeah that doesnt work either, i have no idea what could be wrong. thanks for trying to help anyways.
there might be more going on here wrong than this but you definitely need to use your "set_health" and "get_health" functions of the player class inside the trex function.. ie...

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
void dinosaur::trex(player &P)
{
    int TrexHealth = 0;
    int attacks[5] = {5,9,13,19,23};
    time_t T;
    time(&T);
    srand(T);
    int Time;

    Time = rand() % 6;

    TrexHealth = dinosaurHealth;

    switch(Time)
    {
        case 0:
            {
                cout << "T-Rex used Bite!!" << endl;
                P.set_health(P.get_health() -5);
            }break;
        case 1:
            {
                cout << "T-Rex used Stomp!!" << endl;
                P.set_health(P.get_health() - 9);
            }break;
        case 2:
            {
                cout << "T-Rex used Crunch" << endl;
               P.set_health(P.get_health() - 13);
            }break;
        case 3:
            {
                cout << "T-Rex used Slam" << endl;
                P.set_health(P.get_health() - 19);
            }break;
        case 4:
            {
                cout << "T-Rex used Pulverize" << endl;
                P.set_health(P.get_health() - 23);
            }break;
        default:
            {
                cout << "T-Rex attack missed!!" << endl;
              // No need for anything here
            }
    }
}


you have to do this because health is a private variable of player therefor dinosaur cannot access it directly
Topic archived. No new replies allowed.