Sign in to follow this  
slyprid

Class Arrays in Visual C++ .NET 2003

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


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


Doh! You're absolutely right.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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++.

Share this post


Link to post
Share on other sites
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[i]->move();
if(bullets[i]->Location.Y < -17)
{
bullets[i]->Velocity = 1;
MyForm->Controls->Remove(bullets[i]);
bulletCount--;
}
bullets[i]->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;
};

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
In the class header:

MyStarFighterBullet* bullets[];


In your constructor:

bullets = new MyStarFighterBullet * [5];


That should take care of it.

Share this post


Link to post
Share on other sites
Blah, can't believe I didn't see that. Thanks for your time, that got it to work, now just to get it to move right. Thanks again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this