How to reference an array in a different function

Im trying to reference my array in another function but i keep getting errors.

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
void player::store()
{
    int menuChoice;
    int amountChoice = 0;
    int items[4] = {0,0,0,0};
    string inv;


    cout << "\nWelcome to the store what do you want to buy?" << endl;
    cout << "1) Health Pack - $20" << endl;
    cout << "2) Pistol Ammo - $30" << endl;
    cout << "3) Shotgun Ammo - $50" << endl;
    cout << "4) Rifle Ammo - $80" << endl;
    cin >> menuChoice;

        switch(menuChoice)
        {
            case 1:
                {
                    cout << "How many Health Packs do you want to buy?" << endl;
                    cin >> amountChoice;

                    items[1] = amountChoice;
                    amountChoice = 0;

                }break;

            case 2:
                {
                    cout << "How much Pistol Ammo do you want to buy?" << endl;
                    cin >> amountChoice;

                    items[2] = amountChoice;
                    amountChoice = 0;

                }break;

            case 3:
                {
                    cout << "How much Shotgun Ammo do you want to buy?" << endl;
                    cin >> amountChoice;

                    items[3] = amountChoice;
                    amountChoice = 0;

                }break;

            case 4:
                {
                    cout << "How much Rifle Ammo do you want to buy?" << endl;
                    cin >> amountChoice;

                    items[4] = amountChoice;
                    amountChoice = 0;

                }break;

            default:
                cout << "Thank you for shopping at the store!!" << endl;
        }

        cout << "Items bought:\n" << endl;

        cout << "Health Packs: " << items[1] << endl;
        cout << "Pistol Ammo: " << items[2] << " rounds" << endl;
        cout << "Shotgun Ammo: " << items[3] << " rounds" << endl;
        cout << "Rifle Ammo: " << items[4] << " rounds" << endl;

        backpack();
}

void player::backpack(int &items[4])
{
    cout << items[1] << endl;
    cout << items[2] << endl;
    cout << items[3] << endl;
    cout << items[4] << endl;
}



someone suggested void player::backpack(int (&items)[4])

but i still got errors.

C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|81|error: prototype for 'void player::backpack(int (&)[4])' does not match any in class 'player'|
C:\Users\Chay\Desktop\Dinosaur Arena\player.h|24|error: candidate is: void player::backpack()|
||=== Build finished: 2 errors, 0 warnings ===|

Last edited on
items[] is a local variable in store(). It isn't shared to the calling function and is a totally separate items[] array as what is passed to backpack.

To make it the same, pass it into player::store() as a parameter as you did with player::backpack(), OR better yet, make it a member of class player and remove it from the calling of backpack.
ok i added it to the class and i got these errors

C:\Users\Chay\Desktop\Dinosaur Arena\player.h||In constructor 'player::player()':|
C:\Users\Chay\Desktop\Dinosaur Arena\player.h|56|error: invalid types 'int[int]' for array subscript|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h||In member function 'void player::store()':|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|31|error: invalid types 'int[int]' for array subscript|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|41|error: invalid types 'int[int]' for array subscript|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|51|error: invalid types 'int[int]' for array subscript|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|61|error: invalid types 'int[int]' for array subscript|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|72|error: invalid types 'int[int]' for array subscript|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|73|error: invalid types 'int[int]' for array subscript|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|74|error: invalid types 'int[int]' for array subscript|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|75|error: invalid types 'int[int]' for array subscript|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h||In member function 'void player::backpack()':|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|82|error: invalid types 'int[int]' for array subscript|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|83|error: invalid types 'int[int]' for array subscript|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|84|error: invalid types 'int[int]' for array subscript|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|85|error: invalid types 'int[int]' for array subscript|
||=== Build finished: 13 errors, 0 warnings ===|



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
#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 items;
        //std::vector<std::string> inventory;

    public:
        void save();
        void load();
        void MainGame();
        void store();
        void timer();
        void backpack();
        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_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();
        //std::vector<std::string> get_inventory();
};

player::player()
{
    health = 100;
    pistolAmmo = 17;
    shotgunAmmo = 8;
    rifleAmmo = 30;
    score = 0;
    money = 0;
    items[4] = {0,0,0,0};
}

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 = I;
}


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;
}

#endif // PLAYER_H
Last edited on
replace line 16:
int items;
with:
int items[4];

Then delete line 56 because items[] has already been initialized.
items[4] = {0,0,0,0};

Then, zero items[] in your constructor manually. (4 isn't hard, otherwise loop it, or use a memcpy).

Finally change line 130:
int player::get_items()
to
int* player::get_items()

Oh, and you may want to decide how you want to set items[] in line 94.
Last edited on
change your player function to..

1
2
3
4
5
6
7
void player::backpack(int * items)
{
        cout << "Health Packs: " << items[1] << endl;
        cout << "Pistol Ammo: " << items[2] << " rounds" << endl;
        cout << "Shotgun Ammo: " << items[3] << " rounds" << endl;
        cout << "Rifle Ammo: " << items[4] << " rounds" << endl;
}


and change the prototype to

1
2
3
4
5
6
class player
{
public:
        void store();
        void backpack(int *);
};



and then change the player call at the end of your store() function from
"backpack();"
to "backpack(items);"
it still doesnt work :(

C:\Users\Chay\Desktop\Dinosaur Arena\player.h||In constructor 'player::player()':|
C:\Users\Chay\Desktop\Dinosaur Arena\player.h|56|error: invalid types 'int[int]' for array subscript|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h||In member function 'void player::store()':|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|31|error: invalid types 'int[int]' for array subscript|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|41|error: invalid types 'int[int]' for array subscript|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|51|error: invalid types 'int[int]' for array subscript|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|61|error: invalid types 'int[int]' for array subscript|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|74|error: invalid conversion from 'int' to 'int*'|
C:\Users\Chay\Desktop\Dinosaur Arena\MainGame.h|74|error: initializing argument 1 of 'void player::backpack(int*)'|
||=== Build finished: 7 errors, 0 warnings ===|
That error looks like you are still doing this:
1
2
int items;
items[4] = {0,0,0,0};


if items is an array, you can't define it as an int. Likewise, if items is an int, you can't use it like an array.

PS it's bedtime for me, I'll take a look again tomorrow if your problem isn't solved.
Last edited on
here is an example of code that compiles for me

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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#ifndef PLAYER_H
#define PLAYER_H

#include <vector>
#include <string>
#include <iostream>

using std::endl;
using std::cout;
using std::cin;

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

    public:
        void save();
        void load();
        void MainGame();
        void store();
        void timer();
        void backpack(int *);
        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_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();
        //std::vector<std::string> get_inventory();
};

player::player()
{
    health = 100;
    pistolAmmo = 17;
    shotgunAmmo = 8;
    rifleAmmo = 30;
    score = 0;
    money = 0;
}

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 = I;
}


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;
}

void player::store()
{
    int menuChoice;
    int amountChoice = 0;
    int items[4];
    std::string inv;


    cout << "\nWelcome to the store what do you want to buy?" << endl;
    cout << "1) Health Pack - $20" << endl;
    cout << "2) Pistol Ammo - $30" << endl;
    cout << "3) Shotgun Ammo - $50" << endl;
    cout << "4) Rifle Ammo - $80" << endl;
    cin >> menuChoice;

        switch(menuChoice)
        {
            case 1:
                {
                    cout << "How many Health Packs do you want to buy?" << endl;
                    cin >> amountChoice;

                    items[1] = amountChoice;
                    amountChoice = 0;

                }break;

            case 2:
                {
                    cout << "How much Pistol Ammo do you want to buy?" << endl;
                    cin >> amountChoice;

                    items[2] = amountChoice;
                    amountChoice = 0;

                }break;

            case 3:
                {
                    cout << "How much Shotgun Ammo do you want to buy?" << endl;
                    cin >> amountChoice;

                    items[3] = amountChoice;
                    amountChoice = 0;

                }break;

            case 4:
                {
                    cout << "How much Rifle Ammo do you want to buy?" << endl;
                    cin >> amountChoice;

                    items[4] = amountChoice;
                    amountChoice = 0;

                }break;

            default:
                cout << "Thank you for shopping at the store!!" << endl;
        }

        cout << "Items bought:\n" << endl;

        cout << "Health Packs: " << items[1] << endl;
        cout << "Pistol Ammo: " << items[2] << " rounds" << endl;
        cout << "Shotgun Ammo: " << items[3] << " rounds" << endl;
        cout << "Rifle Ammo: " << items[4] << " rounds" << endl;

        backpack(items);
}

void player::backpack(int *items)
{
    cout << "Health Packs: " << items[1] << endl;
    cout << "Pistol Ammo: " << items[2] << " rounds" << endl;
    cout << "Shotgun Ammo: " << items[3] << " rounds" << endl;
    cout << "Rifle Ammo: " << items[4] << " rounds" << endl;
}

int main()
{
	int buf;
	player p;
	p.store();
	return 0;
	cin >> buf;
}

#endif 
@ dreamincolor

I want all my stuff in seperate files becuase its easier to read and fix.
okay....

main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "player.h"
#include <iostream>

int main()
{
	int buf;
	player p;
	p.store();
	return 0;
	std::cin >> buf;
}



player.h
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
#ifndef PLAYER_H
#define PLAYER_H

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

    public:
        void save();
        void load();
        void MainGame();
        void store();
        void timer();
        void backpack(int *);
        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_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();
        //std::vector<std::string> get_inventory();
};

#endif 


player.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <vector>
#include <string>
#include "player.h"
#include <iostream>

using std::endl;
using std::cout;
using std::cin;

player::player()
{
    health = 100;
    pistolAmmo = 17;
    shotgunAmmo = 8;
    rifleAmmo = 30;
    score = 0;
    money = 0;
}

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 = I;
}


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;
}

void player::store()
{
    int menuChoice;
    int amountChoice = 0;
    int items[4];
    std::string inv;


    cout << "\nWelcome to the store what do you want to buy?" << endl;
    cout << "1) Health Pack - $20" << endl;
    cout << "2) Pistol Ammo - $30" << endl;
    cout << "3) Shotgun Ammo - $50" << endl;
    cout << "4) Rifle Ammo - $80" << endl;
    cin >> menuChoice;

        switch(menuChoice)
        {
            case 1:
                {
                    cout << "How many Health Packs do you want to buy?" << endl;
                    cin >> amountChoice;

                    items[1] = amountChoice;
                    amountChoice = 0;

                }break;

            case 2:
                {
                    cout << "How much Pistol Ammo do you want to buy?" << endl;
                    cin >> amountChoice;

                    items[2] = amountChoice;
                    amountChoice = 0;

                }break;

            case 3:
                {
                    cout << "How much Shotgun Ammo do you want to buy?" << endl;
                    cin >> amountChoice;

                    items[3] = amountChoice;
                    amountChoice = 0;

                }break;

            case 4:
                {
                    cout << "How much Rifle Ammo do you want to buy?" << endl;
                    cin >> amountChoice;

                    items[4] = amountChoice;
                    amountChoice = 0;

                }break;

            default:
                cout << "Thank you for shopping at the store!!" << endl;
        }

        cout << "Items bought:\n" << endl;

        cout << "Health Packs: " << items[1] << endl;
        cout << "Pistol Ammo: " << items[2] << " rounds" << endl;
        cout << "Shotgun Ammo: " << items[3] << " rounds" << endl;
        cout << "Rifle Ammo: " << items[4] << " rounds" << endl;

        backpack(items);
}

void player::backpack(int *items)
{
    cout << "Health Packs: " << items[1] << endl;
    cout << "Pistol Ammo: " << items[2] << " rounds" << endl;
    cout << "Shotgun Ammo: " << items[3] << " rounds" << endl;
    cout << "Rifle Ammo: " << items[4] << " rounds" << endl;
}
Ok i got it to work but i cant include the initializer list or ill get errors but i need it or else i get big numbers, how do i fix this?

this is my fixed 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
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
#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 items[];
        //std::vector<std::string> inventory;

    public:
        void save();
        void load();
        void MainGame();
        void store();
        void timer();
        void backpack();
        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_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();
        //std::vector<std::string> get_inventory();
};

player::player()
{
    health = 100;
    pistolAmmo = 17;
    shotgunAmmo = 8;
    rifleAmmo = 30;
    score = 0;
    money = 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;
}


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];
}

#endif // PLAYER_H
Regarding
1
2
3
int items[];
items[4];
return items[4];


Yikes!
Definition:
int items[];

Use in constructor:
items[4];

Use in set function:
items[4] = I;

Use in get function:
return items[4];

That isn't going to do what you want at all!

To solve your initializer list issue AND to solve your array issue:
Change line 16 to:
int* items;
Change the constructor to:
1
2
3
4
5
6
player::player() 
    : health(100), pistolAmmo(17), shotgunAmmo(8), rifleAmmo(30), score(0), money(0), items(new int[4])
{
    for (int i = 0; i < 4; ++i)
        items[i] = 0;
}


You also want to change your set_items and get_items functions because return items[4] will give a seg fault upon runtime.
Last edited on
This code perfectly works
Here I used pointers which make the work very simple
I think you should read this article, its great and it strengthens your concept on arrays :- http://www.cplusplus.com/doc/tutorial/dynamic/

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
#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 * items; //define a pointer we will make it an array in the constructer
        //std::vector<std::string> inventory;

    public:
        void save();
        void load();
        void MainGame();
        void store();
        void timer();
        void backpack();
        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); // It will now accept arrays in parameters
      //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();  //Added * so that return the pointer to the array i.e the array
        //std::vector<std::string> get_inventory();
};

player::player()
{
    health = 100;
    pistolAmmo = 17;
    shotgunAmmo = 8;
    rifleAmmo = 30;
    score = 0;
    money = 0;
    items = new int[4];  //defining the pointer as an array
}

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) //This will take array as parameters
{
    items = I;
}


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() // Add * to return array
{
    return items;
}

#endif // PLAYER_H 
Last edited on
Ok now i get a ton of errors after making the changes

C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|61|error: prototype for 'int* dinosaur::get_attacks()' does not match any in class 'dinosaur'|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|27|error: candidate is: int dinosaur::get_attacks()|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h||In member function 'void dinosaur::trex()':|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|85|error: 'health' was not declared in this scope|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|90|error: 'health' was not declared in this scope|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|95|error: 'health' was not declared in this scope|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|100|error: 'health' was not declared in this scope|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|105|error: 'health' was not declared in this scope|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|110|error: 'health' was not declared in this scope|
C:\Users\Chay\Desktop\Dinosaur Arena\Dinosaur.h|70|warning: unused variable 'attacks'|
C:\Users\Chay\Desktop\Dinosaur Arena\player.h|140|error: prototype for 'int* player::get_items()' does not match any in class 'player'|
C:\Users\Chay\Desktop\Dinosaur Arena\player.h|48|error: candidate is: int player::get_items()|
||=== Build finished: 10 errors, 1 warnings ===|




I want to use my varibale health from my plyaer class in a function used by dinosaur class but it wont let me


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
#ifndef PLAYER_H
#define PLAYER_H

#include <vector>
#include <string>
#include "Dinosaur.h"

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(new int[4])
{
    for(int i = 0; i < 5; ++i)
    {
        items[i] = 0;
    }
}

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 = 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;
}

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#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(new int[0])
{
    for(int i = 0; i < 5; ++i)
    {
        attacks[i] = 0;
    }
}

dinosaur::~dinosaur()
{

}

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

void dinosaur::set_attacks(int *A)
{
    attacks = A;
}

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

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

int *dinosaur::get_attacks()
{
    return attacks;
}


void dinosaur::trex()
{
    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;
                health -= 5;
            }break;
        case 1:
            {
                cout << "T-Rex used Stomp!!" << endl;
                health -= 9;
            }break;
        case 2:
            {
                cout << "T-Rex used Crunch" << endl;
                health -= 13;
            }break;
        case 3:
            {
                cout << "T-Rex used Slam" << endl;
                health -= 19;
            }break;
        case 4:
            {
                cout << "T-Rex used Pulverize" << endl;
                health -= 23;
            }break;
        default:
            {
                cout << "T-Rex attack missed!!" << endl;
                health -= 0;
            }
    }
}

void dinosaur::velociraptor()
{

}

#endif // DINOSAUR_H_INCLUDED 
Last edited on
bump please help, also i used the new keyword so in my destructor i can put delete items right?
closed account (zb0S216C)
Ch1156 wrote:
attacks(new int[0])

You aren't allocating any memory here. Any attempt to access the allocated memory in any shape or form will lead to an access violation (segmentation-fault).

Ch1156 wrote:
1
2
3
int get_attacks();
// ...
int *dinosaur::get_attacks()

They don't match.

Compiler wrote:
"error: 'health' was not declared in this scope|"

I think you want either "TrexHealth" or "dinosaurHealth".

Ch1156 wrote:
"also i used the new keyword so in my destructor i can put delete items right?"

Yes, unless you want to free the memory before object's destructor is called.

Wazzak
No i am trying to take away the players health thats why i have health in their. dinosaurHealth is a generic variable, since im going to use it for alot of dinosaurs i just make TrexHealth in the Trex function and it equals dinosaurHealth
Hi Ch1156. I felt bad about not being too useful in last nights thread, so I decided to try troubleshooting your code myself.

There were a lot of individual issues, particularly around array usage and of course, the player in class dinosaur problem.

I'm steering you away from dynamic array allocation because the arrays in
your classes are of fixed size (5 elements), so a static array with 5 elements should work fine.

I have your project setup in 3 files only. player.h, dinosaur.h and main.cpp (where the main function is).

I have everything working and you will find a nice test program where a player goes 10 rounds with trex, then an outcome is announced (still alive or dead).
The rand() in trex is working well, so the outcome does vary with each program run.
I hope you can get it working without much trouble.

player.h
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
#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[5];
        //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();// returns pointer to array of 5 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)
{
    for(int i = 0; i < 5; ++i)
    {
        items[i] = 0;
    }
}

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)// was broken
{
    for(int i = 0; i < 5; ++i)
    {
        items[i] = I[i];// items = 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()// broken
{
    return items;//items;
}

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

#endif // PLAYER_H 


dinodaur.h
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
#ifndef DINOSAUR_H
#define DINOSAUR_H


#include <iostream>
#include <ctime>
#include <random>
#include "player.h"
using namespace std;// if you must

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

    public:
        void trex(player& P);
        void velociraptor();
        dinosaur();
        dinosaur(int* A);
        ~dinosaur();

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

        int get_dinosaurHealth();
        int *get_attacks();

};

dinosaur::dinosaur(): dinosaurHealth(100)
{
    for(int i = 0; i < 5; ++i)
    {
        attacks[i] = 0;
    }
}

dinosaur::dinosaur(int* A): dinosaurHealth(100)
{
    for(int i = 0; i < 5; ++i)
    {
        attacks[i] = A[i];
    }
}

dinosaur::~dinosaur()
{

}

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

void dinosaur::set_attacks(int *A)
{
    for(int i = 0; i < 5; ++i)
    {
        attacks[i] = A[i];
    }
}

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

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

int *dinosaur::get_attacks()
{
    return attacks;
}


void dinosaur::trex(player& P)
{
    int TrexHealth = 0;
    TrexHealth = dinosaurHealth;

    switch(rand() % 6)
    {
        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;
            }
    }
}

void dinosaur::velociraptor()
{

}

#endif // DINOSAUR_H 


main
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

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

using namespace std;

int main()
{
    time_t T;// seed rand ONCE
    time(&T);// at start
    srand(T);// of main

    player cptCvMan;
    int attack_strength[5] = {5,9,13,19,23};// to copy values from
    dinosaur Dino_1(attack_strength);

    // A 10 round "fight" (cptCvMan really should learn to defend himself)
    int numAttacks = 0;
    while( ++numAttacks <= 10 && cptCvMan.get_health() > 0 )
    {
        Dino_1.trex( cptCvMan );// perform attack
        cout << "cptCvMan health = " << cptCvMan.get_health() << endl;
    }

    // the outcome
    if( cptCvMan.get_health() > 0 )
        cout << endl << "cptCvMan has survived all 10 attacks!. Play again for 2nd round. " << endl;
    else
        cout << endl << "cptCvMan has perished. Goodbye... " << endl;

   return 0;
}

output, trial 1

T-Rex used Slam
cptCvMan health = 81
T-Rex used Stomp!!
cptCvMan health = 72
T-Rex used Stomp!!
cptCvMan health = 63
T-Rex used Slam
cptCvMan health = 44
T-Rex used Pulverize
cptCvMan health = 21
T-Rex used Bite!!
cptCvMan health = 16
T-Rex used Crunch
cptCvMan health = 3
T-Rex used Bite!!
cptCvMan health = -2

cptCvMan has perished. Goodbye...

trial 2

cptCvMan health = 91
T-Rex attack missed!!
cptCvMan health = 91
T-Rex used Crunch
cptCvMan health = 78
T-Rex used Crunch
cptCvMan health = 65
T-Rex used Stomp!!
cptCvMan health = 56
T-Rex attack missed!!
cptCvMan health = 56
T-Rex attack missed!!
cptCvMan health = 56
T-Rex attack missed!!
cptCvMan health = 56
T-Rex attack missed!!
cptCvMan health = 56
T-Rex used Bite!!
cptCvMan health = 51

cptCvMan has survived all 10 attacks!. Play again for 2nd round.

Hopefully this gets you rolling again.

EDIT: Sorry. Unaware of thread activity. Thought I was posting to follow bump.
Last edited on
closed account (zb0S216C)
I think you need to reconsider your game's design. Here's an example from the top of my head:

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
class Dinosaur;
class CaveBear;

class Player
{
    public:
        Player( )
            : MHealth_( 100 ), MAttackDamage_( 5u )
        { }

    private:
        signed int MHealth_;
        unsigned int MAttackDamage_;

    public:
        void ReceiveDamage( ::Dinosaur &Dino_ );
        void ReceiveDamage( ::CaveBear &Bear_ );
        unsigned int QueryAttackDamage( ) const
        {
            return( MAttackDamage_ );
        }
};

class Dinosaur
{
    public:
        Dinosaur( )
            : MAttackDam_( 5u ), MHealth_( 100 )
        { }

    private:
        unsigned int MAttackDam_;
        signed int MHealth_;

    public:
        unsigned int QueryAttackDamage( ) const
        {
            return( MAttackDam_ );
        }

        void ReceiveDamage( ::Player &Player_ )
        {
            if( ( MHealth_ -= Player_.QueryAttackDamage( ) ) < 0 )
                MHealth_ = 0;
        }

        void AttackPlayer( ::Player &Player_ )
        {
            Player_.ReceiveDamage( *this );
        }
};

class CaveBear
{
    public:
        CaveBear( )
            : MAttackDam_( 10u ), MHealth_( 100 )
        { }

    private:
        unsigned int MAttackDam_;
        signed int MHealth_;

    public:
        unsigned int QueryAttackDamage( ) const
        {
            return( MAttackDam_ );
        }

        void ReceiveDamage( ::Player &Player_ )
        {
            if( ( MHealth_ -= Player_.QueryAttackDamage( ) ) < 0 )
                MHealth_ = 0;
        }

        void AttackPlayer( ::Player &Player_ )
        {
            Player_.ReceiveDamage( *this );
        }
};

void Player::ReceiveDamage( ::Dinosaur &Dino_ )
{

    if( ( MHealth_ -= Dino_.QueryAttackDamage( ) ) < 0 )
        MHealth_ = 0;
}

void Player::ReceiveDamage( ::CaveBear &Bear_ )
{
    if( ( MHealth_ -= Bear_.QueryAttackDamage( ) ) < 0 )
        MHealth_ = 0;
}

No setters and the code speaks for itself. Though, because you're not using polymorphism, the code isn't exactly flexible.

Wazzak
Last edited on
@fun2code if you declare items as a pointer and then initialize it as an integer array of 5 elements then you could just do items = I in the set items also you could delete it in the destructer.

@ch1156 you did everything right but you just messed up with the definition and the declaration, they have to be the same. And in function get_attacks() replace health with dinosaurHealth, it will work and also you should atleast read and try to understand your errors before posting them blindly. You could also delete items if you initialized it as a pointer.
Last edited on
Topic archived. No new replies allowed.