Sprite1 *enemy = new Sprite1(100, 100, "enemy.bmp", *screen);
enemy->DrawJon(screen);
enemyList.insert(enemyList.end(), *enemy);
For some reason, the enemy is not being stored in the list. Any ideas?
EDIT:
Also how to access the objects and use them after wards like:
for (int g=0; g<enemyList.size(); g++)
{
Sprite1 enemyToMove = enemyList.at(g); //.at(g);
enemyToMove.MoveJon(0, 50, screen);
}
EDIT:
So even though this got closed I still want people to know what the solution was. I had not made a constructor that set the properties on the class. In the end I was able to get this working with a default constructor and a setter method
It inserts a copy of the object before the end of
enemyList. For this, you should really preferenemyList.push_back(*enemy). However, note that it is a copy. If you want to be referring to the same object inside and outside of thestd::vector, you’re going to want a vector of pointers (preferably smart pointers). For example:Alternatively, use a
std::vector<std::reference_wrapper>.If you have changed your
std::vectorto contain pointers orstd::reference_wrappers, you can just access the enemies like so:If you’re still containing
Sprite1s directly, you need to keep a reference to the object returned byoperator[]orat, otherwise you’ll copy it: