# C++ - A collection of abstract classes/different types of classes?

This topic is 2298 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm a beginner in C++, for this game project that I am creating what I'd like to have is a collection of generic "GameObject" element, each with virtual methods for Update and Draw...then have classes inheriting from it with their own definitions of such methods so that when updating the world I only need to keep one collection to cycle through...

It seems like it is impossible with Vector to hold a collection of abstract classes, I tried instead to keep the Draw and Update methods virtual, but then if I create an Enemy : public GameObject instance and add it to a Vector<GameObject> and call update then I get all sorts of Linker: undefined symbol errors....

I don't know, there's something I am not grasping.... I am sure this is a type of setting that many application would require.... what is your advice on approaching this? Should I maybe try a different type of collector than Vector?

Thank you very much

##### Share on other sites
You could use a vector of GameObject (smart) pointers.

##### Share on other sites
To use polymorphism like that, you'll need to use pointers. Make your vector a type of [font="Courier New"]std::vector<GameObject*>[/font] and add elements to it via new.

IMPORTANT NOTE: What I just suggested about with[font="Courier New"] std::vector<GameObject*>[/font] requires you to use raw pointers. Personally, I would suggest using smart pointers. If you don't have a compiler that supports [font="Courier New"]std::shared_ptr[/font], I would recommend [font="Courier New"]boost::shared_ptr[/font]. Then the vector would be defined as [font="Courier New"]std::vector<std::shared_ptr<GameObject> >[/font].

SiCrane strikes again!

##### Share on other sites
Thanks for the comments! I tried doing that with regular pointers, still getting the same following errors:

 Error 25 error LNK2001: unresolved external symbol "public: virtual void __thiscall Enemy::update(float)" (?update@Enemy@@UAEXM@Z) Error 28 error LNK2001: unresolved external symbol "public: virtual void __thiscall GameObject::update(float)" (?update@Object3DS@@UAEXM@Z) 

My code was:

 class GameObject{ .... virtual void update(float t): .... } 

 class Enemy: public GameObject{ ..... void update(float d); ..... void Enemy::update(float d){ //Have a breakpoint here } 

 class World{ ... vector<GameObject*> gameObjects; ... } 

 class Level{ World levelWorld; .... Level(){ levelWorld = new World(); } ... Enemy* en = new Enemy(); levelWorld->visibleObjects.push_back(en); levelWorld->visibleObjects.at(0)->update(0); } 

I guess i need to try with auto_ptr.... But is there anything wrong with this code?

##### Share on other sites
I'm not sure what's up with the missing Enemy::update() linker error, but you don't seem to supply a GameObject::update() member function definition. If you don't want to supply a definition for a base class virtual function because every derived class is supposed to supply their own definition, then you can make it pure virtual by appending = 0 after the declaration before the semicolon.