I have a list of pointers to a base abstract class (Entity)
std::list<Entity*> m_entities;
I have created a typedef for iterating through this class
typedef std::list<Entity*>::const_iterator entityIter;
I then try and iterate through each pointer in the list
for (entityIter i = m_entities.begin(); i != m_entities.end(); ++i)
{
const Entity &e = *i; // ERROR
e.DoStuff();
}
I get the following error when attempting to reference each pointer
IntelliSense: no suitable constructor exists to convert from “Entity *const” to “Entity”
What have I done incorrectly?
EDIT:
I have tried to use std::shared_ptr
std::list<std::shared_ptr<Entity>> m_entities;
I can’t add to the list this way though
Entity::Entity(Game *game)
: m_game(game)
{
m_game->g_idGenerator->generateNewID();
m_game->m_entities.push_back(this); // ERROR
}
Using the following
m_game->m_entities.push_back(std::shared_ptr<Entity>(this));
gives me this error
error C2664: ‘void std::list<_Ty>::push_back(_Ty &&)’ : cannot convert parameter 1 from >’Entity’ to ‘std::tr1::shared_ptr<_Ty> &&’
EDIT 2:
Current code summary
for (entityIter i = m_entities.begin(); i != m_entities.end(); ++i)
{
// *i dereferences the iterator and returns an Entity*
// **i would additionally deference the pointer
// Adding 'const' infront of Entity means that I can't alter the Entity
Entity &e = **i;
e.draw(dt); // Causes access violation error with standard pointers
}
Have tried converting to std:shared_ptr to see if it would avoid the error triggered by the code above.
However, I am now having trouble adding the Entity to the list of std::shared_ptr
m_game->m_entities.push_back(std::shared_ptr<Entity>(this));
So in summary I have the access violation error with a standard pointer and I can’t add to the list with a shared_ptr.
Populating the list is done via the constructor of the base Entity class
Entity::Entity(Game *game)
: m_game(game)
{
m_game->g_idGenerator->generateNewID();
// shared_ptr version
m_game->m_entities.push_back(std::shared_ptr<Entity>(this)); // ERROR C2664
// raw pointer version
//m_game->m_entities.push_back(this); // ACCESS VIOLATION ERROR when calling methods
}
*idereferences the iterator and returns an Entity*.**iwould additionally deference the pointer.To avoid keeping the reference around, you can use
(*i)->memberFunction(...);Don’t forget that if you allocated the Entity’s with
operator newthat you also need tooperator deletethem.Here is an example using
std::shared_ptrsince your code is looking to complex to discuss on a page.I created a trivial
Entityclass and used it instd::shared_ptr. I put the samestd::shared_ptr<Entity>multiple times purely for demonstration thatstd::shared_ptrmanages this information including copying itself into the list.Note: There is a difference between you putting the exact same raw pointer into multiple
std::shared_ptrobjects and copying astd::shared_ptraspush_backdoes. The latter case is fully managed bystd::shared_ptrwhile the former case has eachstd::shared_ptrattempting to manage independently.