How do i put a vector in a class?

I tried putting a vector in my class but it wont let me do it it keeps giving me errors. here is my 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
#ifndef PLAYER_H
#define PLAYER_H

#include <vector>

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

    public:
        void save();
        void load();
        void MainGame();
        void store();
        void inventory();
        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);

        int get_health();
        int get_pistolAmmo();
        int get_shotgunAmmo();
        int get_rifleAmmo();
        int get_score();
        int get_money();
};

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

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

#endif // PLAYER_H



errors:

C:\Users\Chay\Desktop\Dinosaur Arena\player.h|15|error: ISO C++ forbids declaration of 'vector' with no type|
C:\Users\Chay\Desktop\Dinosaur Arena\player.h|15|error: expected ';' before '<' token|
||=== Build finished: 2 errors, 0 warnings ===|
vector<string>;
1. Try with std::vector<std::string>
2. Give the variable a name

Example:
std::vector<std::string> MyVector;

Ch1156 wrote:
C:\Users\Chay\Desktop\Dinosaur Arena\player.h

Woah, Dinosaur Arena with
1
2
3
int pistolAmmo;
int shotgunAmmo;
int rifleAmmo;

and
int money;!!
Can't wait.
Last edited on
I tried that and i get

C:\Users\Chay\Desktop\Dinosaur Arena\player.h|15|error: ISO C++ forbids declaration of 'vector' with no type|
C:\Users\Chay\Desktop\Dinosaur Arena\player.h|15|error: invalid use of '::'|
C:\Users\Chay\Desktop\Dinosaur Arena\player.h|15|error: expected ';' before '<' token|
||=== Build finished: 3 errors, 0 warnings ===|
Try including string #include <string>
ok it worked, but why do i need std:: if i already included vector and string?
I don't know if you really needed std::, but unless you write 'using namespace std;' at the beginning, after the inclusions, you need to explicit your need of the std namespace like that.

Example:

1
2
3
4
5
6
7
8
// Explicit Namespace
#include <vector>
std::vector<int> a;

// Implicit Namespace
#include <vector>
using namespace std;
vector<int> a;
Last edited on
also how would i put it in my function parameters like void set_money(int M);?

Ch1156 wrote:
also how would i put it in my function parameters like void set_money(int M);?

What do you mean?
never mind i figured it out, i did this:

void set_inventory(std::vector<std::string> &inventory);
std::vector<std::string> get_inventory();
now what do i put in

1
2
3
4
5
6
7
8
9
player::player()
{
    health = 100;
    pistolAmmo = 17;
    shotgunAmmo = 8;
    rifleAmmo = 30;
    score = 0;
    money = 0;
}


for the vector? its supposed to store the items that you buy from the shop. do i use a setter and getter for the vecotr?
Last edited on
In the constructor, you need nothing, it's a class and it gets automatically initialized.
Also, I suggest you to use 'AddToInventory', 'IsInInventory' and 'RemoveFromInventory' instead of SetInventory and GetInventory.
Topic archived. No new replies allowed.