Initialize an array of enemy pointers

Why can’t I do this?

Enemy* enemies[]{
        &Enemy{Vector2{(32 * 24), (32 * 24)},goblin_idle,goblin_run},
        &Enemy{Vector2{(32 * 50), (32 * 50)},slime_idle,slime_run},
        &Enemy{Vector2{(32 * 1), (32 * 35)},goblin_idle,goblin_run},
        &Enemy{Vector2{(32 * 75), (32 * 90)},slime_idle,slime_run},
        &Enemy{Vector2{(32 * 90), (32 * 40)},goblin_idle,goblin_run}
};

I get the taking address of rvalue [-fpermissive] error.

replace & with new and it should work. The & operator gets the address of a variable already created. With the new keyword, we’re creating an object and automatically returning its memory address, ready to be stored in the array.

You will need to make sure you delete those pointers when you exit or close the game.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms