I just want to start by saying I'm still decently new to coding, and I've been trying to do research but just got stuck, as I can't find exactly what I am looking for.
I am trying to make my first game, a simple text adventure game. I was trying to make a player's inventory so I started using a vector of custom classes. After some research I found that I needed to make a base class of multiple classes.
Just for reference, I am using
vector<item*> inventory;
class Map : public item
{
public:
void print();
virtual string getName(){return name;};
private:
string name = "Map";
};
class Compass : public item
{
public:
virtual string getName(){return name;};
private:
string name = "Compass";
};
class item
{
public:
virtual string getName(){return name;};
private:
string name = "item";
};
this is in my header file, and I have cpp
files that expand on it.
When storing it I found out that I have to add virtual functions to get the name, so I added a name data member and made a getName()
function and made it virtual. Eventually I was able to pull the items out and get the name, but then I found a new problem I couldn't find the answer to.
EDIT:
after using inventory.push_back(&map)
, trying to call (inventory[0])->print()
gives me the error of "no member of print in item"
end edit
My map
class has a print()
function, and i need to use it from the vector. Would I have to create a virtual print()
function in my item
class just so it can transfer down using the virtual part, or is there an easier way of doing this?
sorry if this is simple or a stupid fix, when I was doing research all I found was people trying to find names and things, but i couldn't find how to access a method without having it in the base class.