I am trying to dynamically allocate array of objects. I have a class Enemy in my project and I'd like to create 5 enemies. After exhausting research I can't get answer for my problem.
Enemy* mainEnemy = new Enemy(enemyShip)[5];
There is an error : no suitable conversion from Enemy to Enemy* exists.
When I deleted the pointer, there is no errors in this line :
Enemy mainEnemy = new Enemy(enemyShip)[5];
But how can I work with objects created like this? I can't set position of single object like that:
mainEnemy[0]->StartingPosition(); // no operator "[]" matches these operands . operand types are: Enemy[int]
enemyShip? What are you trying to accomplish withnew Enemy(enemyShip)[5];? Allocate an array of 5 enemies, all of which are constructed fromenemyShip?std::vector<Enemy> mainEnemy(5, enemyShip);&enemyTis suspicious, as we don't know where or when thisenemyTobject is created or its lifetime. Needless to say, storing addresses of anything means that you need to make sure that whatever you're storing the address of must not go out of scope during the time you are accessing this address.