Pointer Array?

Hi guys. Just wondering what I'm doing wrong.

this is in my header file
1
2
#define MAX_ENEMY_SCOUT 3
CEnemy* Enemy_Scouts[MAX_ENEMY_SCOUT];


this is in my cpp file
1
2
3
4
5
6
Enemy_Scouts = new CEnemy[MAX_ENEMY_SCOUT];

for (int i = 0; i < MAX_ENEMY_SCOUT; i++)
{
	Enemy_Scouts[i]->SetHealth(100); //100 hp for each enemy scout by default
}


and the error i got was "error C2440: '=' : cannot convert from 'CEnemy *' to 'CEnemy *[3]'"

I thought i was doing it right? What can i do to make the array of pointers? Cause I dont want to use do array of objects.
And since my intellisense works(eg. when i type "Enemy_Scouts[i]->" the members of the class shows up while when i type "Enemy_Scouts[i]. "the members of the class do not show up ), i figured it should work
I thought i was doing it right? What can i do to make the array of pointers? Cause I dont want to use do array of objects.


Enemy_Scouts is an array of pointers. It shouldn't be defined in a header file, but that doesn't contribute to the current problem you're experiencing.

This: Enemy_Scouts = new CEnemy[MAX_ENEMY_SCOUT]; attempts to set your array equal to a pointer. It's an array of pointers. It is not, itself, a pointer.

The pointers in the array don't point to valid memory of course. That's the job you need to accomplish. If each pointer is supposed to point to 1 CEnemy, you might do the following:

1
2
 for (unsigned i=0; i<MAX_ENEMY_SCOUT; ++i)
    Enemy_Scouts[i] = new EnemyScout ;


Of course, this isn't very C++ish code with the raw pointers and manual memory management.
Hi Cire,

i changed the code to
1
2
3
4
5
for (int i = 0; i < MAX_ENEMY_SCOUT; i++)
	{
		Enemy_Scouts[i] = new CEnemy();
		Enemy_Scouts[i]->SetHealth(100); //100 hp
	}


but i still have the same error. the error "error C2440: '=' : cannot convert from 'CEnemy *' to 'CEnemy *[3]' " seems to point to Enemy_Scouts = new CEnemy[MAX_ENEMY_SCOUT];

Is the statement Enemy_Scouts = new CEnemy[MAX_ENEMY_SCOUT]; valid? Cause I do want to make a pointer of arrays with each element pointing to a CEnemy

And thanks for responding :)
CEnemy Enemy_Scouts[MAX_ENEMY_SCOUT]; there, you are done.

> Cause I do want to make a pointer of arrays with each element pointing to a CEnemy
┬┐why?
1
2
3
4
CEnemy* *Enemy_Scouts;
Enemy_Scouts = new CEnemy*[MAX_ENEMY_SCOUT];

Enemy_Scouts[0] = new Light_Cavalry;


Or you could use std::vector
1
2
3
4
std::vector<CEnemy> Enemy_Scouts; // std::vector<CEnemy*> if you want polymorphism
Enemy_Scouts.reserve( MAX_ENEMY_SCOUT );

Enemy_Scouts.push_back( CEnemy() );
Haha thanks I solved it already but I forgot to post it here. But yeah ne555 thats how i did it thx
Topic archived. No new replies allowed.