Objects in vectors - need help

I have a player class which has a function for a vector inventory. I have a seperate class for creating equipment (Sword, Shield, etc.). I'm unsure of how to "add" the equipment to the vector so that it is available on request to display that it is indeed in the players inventory. I'm not sure what I can do to accomplish this? Any help is GREATLY appreciated!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#ifndef EQUIPMENT_H
#define EQUIPMENT_H

class equipment
{
	public:
           int damage, armor;
           

		equipment(int setdamage, int setarmor);

};

#endif


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <vector>
#ifndef PLAYER_H
#define PLAYER_H


class player
{
	public:
           int attackPoints, armorPoints;
           std::string username;
           std::vector<std::string> inventory;
           
           void displayInventory();

		player();

};

#endif
I think you need this:
 
std::vector<std::string> inventory;

to be more like this:
 
std::vector<equipment> inventory;


then add have methods in yuour player class like AddItem and RemovItem.

In fact, i'd rename your equipment class to be called "Item".

Ohh, how does the equipment inside the <> tags work? Is that declaring a vector that i'll be storing objects of class equipment in? Or something else?

Also, To do that will I need to include the "equipment.h" in "player.h"?
Last edited on
how does the equipment inside the <> tags work? Is that declaring a vector that i'll be storing objects of class equipment in?

Yes.
will I need to include the "equipment.h" in "player.h"?

Yes, or make sure it's included before player.h in your .cpp files.

Great! I'm a little confused as to objects, how would you create a function that takes a object as parameter? Through a pointer? Or something else?

like...
1
2
3
4
5

void player::addItem(char* sword)
{
inventory.push_back(sword);
}




or something like that?
Not quite. You need to create an equipment instance and pass that to inventory.push_back.

1
2
3
4
5
6
7
8
9
10
11
12
13
class equipment
{
public:
    string equipname;
    int damage, armor;
           
    equipment (string name, int setdamage, int setarmor);
};

void player::addSword ()
{   equipment sword ("sword", 10, 10);  // construct an equipment instance
    inventory.push_back (sword);
} 

I'm getting an error at line 26, not sure why
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
#include <iostream>
#include <vector>
#include "equipment.h"
#include "player.h" 




player::player()
{

}

void player::displayInventory()
{
     if ( inventory.empty() )
     {
          std::cout << "You have nothing in your inventory." << std::endl;
     }
     else
     {
         std::cout << "You have " << inventory.size() << " items in your inventory." << std::endl;
         
         for ( int x = 0; x < inventory.size(); x++ )
         {
             std::cout << inventory[x] << std::endl;
         }
     }
}

void player::addSword()
{    

     
}

void player::removeItem()
{
     
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
#include "equipment.h"
#ifndef PLAYER_H
#define PLAYER_H


class player
{
	public:
           int attackPoints, armorPoints;
           std::string username;
           std::vector<equipment> inventory;
           
           void displayInventory();
           void addSword();
           void removeItem();

		player();

};

#endif 
Last edited on
std::vector doesn't have an overload for operator << so you can't use inventory directly in a cout statement.

Since the members of equipment are public (not recommended), you can do the following:
 
    std::cout << inventory[x].equipname << std::endl;







if they weren't public, how would you accomplish that?
By providing getters and setters.

1
2
3
4
5
6
 
string equipment::getName () const
{  return eqipname;
}

std::cout << inventory[x].getName() << std::endl; 






Last edited on
Topic archived. No new replies allowed.