How do I equal a class object to a pointer of a class object?

**This program is only an example of my real program to show what the problem is**

I'm having a bit of a problem since I can't send the value of a pointer object to a non-pointer object. I made an inventory using a vector pointer vector<Item *> items; and I am trying to pass a value of that array to an Item object of a class.

As you can see by my program, I need to access a child class from the parent class (I actually have much more child classes on my real program) in order to have an inventory of "Item" objects instead of having several arrays of different kinds of classes. Therefore, I figured the best way to do it was using a vector array, such as the one that is shown.

So that works fine and I like how polymorphism works, but the issue rises when I want to set an "Item" object on my player class from the vector pointer array (see line 69 & 70 & 87). You see, I want to move an item from the pointer array to the object in my player class.

Is there any way I can equalize the two values without having to make my object in the Player class a pointer itself? Would it keep the value of attack?

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
#include <iostream>
#include <string>
using namespace std;

//PARENT CLASS
class Item
{
   private:
      string name;
      int price;
   public:
      Item()
      {
         name="Empty";
         price=0;
      }
      Item(string n, int p)
      {
         name=n;
         price=p;
      }

      void setName(string n)
      { name=n; }
      void setPrice(int p)
      { price=p; }
      //Access to child function
      virtual void setAttack(int a){}

      string getName()
      { return name; }
      int getPrice()
      { return price; }
      //Access to child function
      virtual int getAttack() const
      { return 0; }
};

//CHILD CLASS
class Weapon:public Item
{
   private:
      int attack;
   public:
      Weapon()
      { attack=0; }
      Weapon(string n, int p, int a):Item(n,p) //Initializes the parent class
      ( attack=a; }

      void setAttack(int a)
      { attack=a; }
      int getAttack() const
      { return attack; }
};

class Player
{
   private:
      string playerName;
      Item weapon;  //I could change this to a pointer and it would work
   public:          //But I want to see if I can avoid that some way
      Player()
      { playerName="Empty" }
      Player(string pn)
      { playerName=pn; }

      void setPlayerName(string pn)
      { playerName=pn; }
      void setWeapon(Item *w)
      { weapon=w; } //ERROR, can't equal a pointer to a non-pointer! 

      string getPlayerName()
      { return playerName; }
};

int main()
{
   //This sets up the inventory
   vector<Item *> items;

   //This is how I would set up an item
   items.push_back(Weapon("Iron Sword",100,50));

   Player user;

   //Can't do this without changing the Item in my Player class to a pointer
   user.setWeapon(items[0]);

}


Any other advice would be welcome too, regarding my way of making the inventory.
Last edited on
If you want to keep polymorphimsm, the variable at line 60 does need to be a pointer (or be a reference, which won't play well with your setWeapon member function). Sorry.

-Albatross
Do you suggest a better way to make an inventory when there are about 5 child functions for the different kinds of "Items"?
Topic archived. No new replies allowed.