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

Started by
3 comments, last by SiCrane 12 years, 5 months ago
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
Advertisement
You could use a vector of GameObject (smart) pointers.
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].

[edit]

SiCrane strikes again!
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
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?

Thanks for your help!
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.

This topic is closed to new replies.

Advertisement