SFML: Classes list won't work.

Hi, so im trying to create a enemy entities class, which i made but i get errors here.

1
2
3
4
5
6
7
8
for(int i = 0; i < sizeof(enemyList); i++)
        {
            enemyList[i].entRect = {blueRect};
            enemyList[i].entsprite.setTexture(texture);
            enemyList[i].entsprite.settTextureRect(enemyList[i].entRect);
            enemyList[i].setPosition((i * 10) + 200, 200);
            enemyList[i].health = 10;
        }


I want to make it possible to access specific members of the Enemies class by using a list but it seems its imspossible as it gives error: no match for 'operator[]' in [enemyList[i]'.
So my question is how can i make it possible to change different enemy class objects health, textures and other members of the class ? (sprite, texture, etc.).
for starters... sizeof() is not what you want.

Also... is this std::list? If that's the case you can't access them by index, but rather you need to use iterators to step through.

Instead of a list, you might want to use a vector:

1
2
3
4
5
6
7
8
std::vector< whatever_enemy_type > enemyList;

enemyList.push_back( ... );

for(size_t i = 0; i < enemyList.size(); ++i)  // note enemyList.size.... do not use sizeof()
{
    enemyList[i].whatever = whatever;
}
But with use of vectors will i be able to dynamically add enemies ? Increase amount of enemies, and size of the vector ?
yes. vectors are basically resizable arrays.

Although if you are inserting/removing elements from the middle of the collection instead of just at the end... then list might be a better way to go.
Last edited on
Topic archived. No new replies allowed.