Class Arrays in Visual C++ .NET 2003

Started by
11 comments, last by Dave Hunt 18 years ago
I'm trying to implement an array of classes into a project I have for one of my courses, but everytime I try to contstruct them it gives me an Object reference not set to an instance of an Object. It seems logical that it should work, but not sure what I am doing wrong. Here is what I got. Prototype of array in header: MyStarFighterBullet* bullets[]; void MyStarFighterController::fire() { bullets[bulletCount] = new MyStarFighterBullet(resources,ship->Left + (ship->Width / 2) - 7, ship->Top - 17); MyForm->Controls->Add(bullets[0]); bulletCount++; } It crashes in between the construct and the add. Did a step by step debug and it was showing it constructing but nothing was being passed to it. The logic behind this is to create an array of all my bullets, add them to the form, and using the timer control I could control the movement of each bullet by referencing it by an index number through the array.
Advertisement
You have to allocate the array before you can use it:
MyStarFighterBullet* bullets[] = new MyStarFighterBullet[MAX_BULLETS]; // assumes MAX_BULLETS is defined

Of course, rather than a fixed-size array, you could use a dynamic collection (std::vector or similar).
Quote:Original post by Dave Hunt
You have to allocate the array before you can use it:
MyStarFighterBullet* bullets[] = new MyStarFighterBullet[MAX_BULLETS]; // assumes MAX_BULLETS is defined
It would probably need to be MyStarFighterBullet **bullets = new MyStarFighterBullet*[MAX_BULLETS];, instead.

Quote:Of course, rather than a fixed-size array, you could use a dynamic collection (std::vector or similar).
Agreed.


jfl.
Quote:Original post by jflanglois
It would probably need to be MyStarFighterBullet **bullets = new MyStarFighterBullet*[MAX_BULLETS];, instead.


Doh! You're absolutely right.
Wow, can't believe I missed that. Thanks. Still got an error but was easy to change. Got a managed error off it so changed it to.

MyStarFighterBullet* bullets[] = new MyStarFighterBullet * [MAX_BULLETS];

Thanks again,
Slyprid
Quote:Original post by slyprid
Wow, can't believe I missed that. Thanks. Still got an error but was easy to change. Got a managed error off it so changed it to.

MyStarFighterBullet* bullets[] = new MyStarFighterBullet * [MAX_BULLETS];

Thanks again,
Slyprid
You're welcome. I wasn't sure whether you were writing in Managed C++ or not, thusly the double pointer. I guess the syntax you used is valid in MC++.
Well i'm running into a bit of different issue now. Well the array and such is allocating but I have a timer that should move the bullet, but when it is called it is losing scope of the array. I tried making it static, it worked to where the scope got passed over to the timer, but it wouldn't move any. Thought static allowed the object stay created until the application was destroyed. Here is what I got.

MyStarFighterController::MyStarFighterController(System::Resources::ResourceManager *res, System::Windows::Forms::Form* frm){     MyStarFighterBullet *bullets[] = new MyStarFighterBullet * [5];     resources = res;     MyForm = frm;     bulletCount = 0;     bulletVelocity = 1;     bFire = false;};void MyStarFighterController::fire(){     if(bulletCount < 5)     {          bFire = true;	 bullets[bulletCount] = new MyStarFighterBullet(resources,ship->Left + (ship->Width / 2) - 7, ship->Top - 17);	 MyForm->Controls->Add(bullets[bulletCount]);	 bulletCount++;     }	}void MyStarFighterController::handleTick(){     if(bFire)     {          for(int i = 0; i < bulletCount; i++)	 {                bullets->move();                if(bullets->Location.Y < -17)	       {	            bullets->Velocity = 1;		   MyForm->Controls->Remove(bullets);		   bulletCount--;	       }	       bullets->Velocity;	 }	 if(bulletCount == 0)	 {	      bFire = false;	 }     }}void MyStarFighterBullet::move(){	int newx, newy;	newx = this->Location.X;	newy = this->Location.Y + (-1 * Velocity);	Point newloc = Point(newx,newy);	this->Location = newloc;};
Oh yeah i'm using Managed C++ atm.
You've made the bullets array local to your constructor. Move the declaration into your class and just do the allocation in the constructor. Then all of your methods will have access to it.
I have a decleration in the class prototype, but if I move over the
MyStarFighterBullet *bullets[] = new MyStarFighterBullet * [5]; to the prototype it tells me
error C3845: 'StarFighter::MyStarFighterController::bullets': only static data members can be initialized inside a __gc class or value type

Thats why I tried the static, while it did work, I wasn't able to change anything. Is it because its trying to create a non-managed array, or something else?

This topic is closed to new replies.

Advertisement