Home How do I store and use different classes in a vector in c++
Reply: 0

How do I store and use different classes in a vector in c++

user11970
1#
user11970 Published in September 21, 2018, 8:04 am

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.

share|improve this question
  • 1
    Please give a Minimal, Complete, and Verifiable example. Your last few paragraphs should also show the code that you are describing in words. – Code-Apprentice Feb 12 at 19:33
  • It depends what you want print to do. If you just want the name then you can just use the getName function you already have. – NathanOliver Feb 12 at 19:33
  • 3
    Use std::vector<std::shared_ptr<Base>> v; while following the polymorphic paradigm. – Ron Feb 12 at 19:33
  • 3
    "my map class has a print() function, and i need to use it from the vector." If print does not make sense for all items, you have no business calling it from a std::vector<item*>. Rethink your design. – Rotem Feb 12 at 19:36
  • 4
    You fell into the beginner trap of wanting to store different types in you containers which leads to (possibly even manual) dynamic memory allocations, pointer semantics, not knowing what objects you have in your container, casts and a bunch of other nasty stuff. Rethink your premise. Don't put different types in the same container and everything will become much easier. – nwp Feb 12 at 19:49

1 Answer 1

active oldest votes
up vote 0 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.404489 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO