List of objects from a base class

I have this base class called Entity, and a class derivied from that called Player. In my MainGamestate class I have a list of Entity.

Why isn't this line working?

1
2
Player player1;
	entities.push_back(player1);


ERROR: 'void std::list<_Ty>::push_back(_Ty &&)' : cannot convert parameter 1 from 'Player (__cdecl *)(void)' to 'std::tr1::shared_ptr<_Ty> &&'.

Entity declaration: std::list<std::shared_ptr<Entity>> entities;
Last edited on
Because your vector conain type std::shared_ptr<Entity> and you trying to pass Player to it. Wrap it in shared_ptr<Entity> first
Thanks for answering!

How would I do that?
Last edited on
entities.push_back(std::make_shared<Player>());
Last edited on
Well, it was you who created vector of shared pointers, so you should know at least this.
1
2
3
4
std::shared_ptr<Entity> x(&player1);
entities.push_back(x);
//or
entities.emplace_back(std::shared_ptr<Entity>(&player1));
Whoa! Using shared_ptr to point to objects not been created with new is probably not such a good idea. It will fail to work later when it tries to delete it.
Topic archived. No new replies allowed.