user2966798
user2966798

Reputation: 5

c++ cannot create object with identifier

I already searched everywhere and I think this is really basic problem but I checked some other people code and they were using the same method to create an object with a unique ID that's why I'm not understanding why it doesnt work with me.

So here's the class Enemy:

class cEnemy{
public:
       //code that doesnt matter
};

So what i want to do is basically create 10 Enemies and each one will have a unique identifier(0...9), so what im doing is:

for (int i = 0; i < 10; i++){
       Enemy[i] = new cEnemy;
}

Right now it already gives me an error: error C2065: 'Enemy' : undeclared identifier

But if instead of writing Enemy[i] if i write Enemy[5] it works fine. I think im missing something.

Why? I saw this code exactly the same in other application and it works...

So my objective as I said it's to create 10 enemies with unique Id's and then to have access to each one but as you see i can't even create them.

Thanks in advance.

PS: The class and main are in the same cpp file

Upvotes: 0

Views: 87

Answers (1)

nhgrif
nhgrif

Reputation: 62052

std::array<cEnemy, 10> Enemy;
for(int i = 0; i<10; ++i) {
    Enemy[i] = new cEnemy;
}

You have to create an array before you can use it. The error you're getting is the same as if you were try to do:

arrInt[i] = someInt;

Before having done:

int arrInt[someCount];

Upvotes: 1

Related Questions