Default constructor causing game to crash

Started by
1 comment, last by Sanadan 20 years, 8 months ago
This is using the WIN32 API in C++ and is the Asteroids clone I am working on in my spare time as a learning project and I have a problem that I''ve narrowed down to being a default constructor issue (I think). I am also using Dev-C++ as a compiler. For reference here is my simple class structure. Base Class: GameObject Derived Classes: SpaceShip Bullet (only used within the SpaceShip class) Asteroid Here are my constructors (just the definitions and initialization lists, because my problem is within the initialization list of the SpaceShip class, Asteroid constructor irrelevant so I left it out but it''s very symetrical to the constructors I have posted):

// gameobject.h

GameObject( const HWND& h = NULL,
                    const HDC& backBufferDC = NULL,
                    const int& numVert = 0); // constructor

// gameobject.cpp

//************************************************************************//

// Constructor:  Initializes all of the variables of the class.

//************************************************************************//

GameObject::GameObject( const HWND& h,
                        const HDC& backBufferDC,
                        const int& numVert) 
    :   numVertices(numVert), 
        // need to use initialization list to setup memory for arrays

        // because i''m using a const pointer to emulate an array

        object(new VECTOR[numVertices]),
        oldObject(new VECTOR[numVertices])
// VECTOR is a user defined mathematical vector class I found elegant code for from GamaSutra.com




// spaceship.h

SpaceShip(HWND h = NULL, HDC backBuffDC = NULL); // constructor

// spaceship.cpp

//************************************************************************//

// Constructor:  Initializes all of the variables of the class.

//************************************************************************//

SpaceShip::SpaceShip(HWND h, HDC backBufferDC)
// initialization list to set the value for the const var''s

   :    aMag(1.0f), vMax(15.0f), maxBullets(5), bulletIndex(0),
        GameObject(h, backBufferDC, 3),
        v(),
        bullets(new Bullet[maxBullets])

// bullet.h

Bullet( const HWND& h = NULL, const HDC& backBufferDC = NULL);
// bullet.cpp

Bullet::Bullet( const HWND& h,
                const HDC& backBufferDC)
    :   GameObject(h, backBufferDC, 1),
        radius(5), start(), v(), maxV(0), isAlive(false)
So... my problem is in the SpaceShip initialization list, specifically with the line
bullets(new Bullet[maxBullets]) 
Obviously my goal with that line of code is to create an array of size maxBullets and in my header file for spaceship bullets is declared:
Bullet *const bullets; 
Now, when the bullets line in the initialization list is included (not commented out) everything compiles perfectly fine with zero errors or warnings, but when I run the application I get a window for a split second and then it closes without doing anything. If I comment that line of code out, everything complies perfectly fine and the game runs as it should in it''s current state. Now, from what I understand, and correct me if I''m wrong, my method of creating the bullets array will call the default constructor for each object that I created in the array. What I mean is the line:
bullets(new Bullet[maxBullets]) 
creates an array of size maxBullets and calls the default constructor for Bullet for each object within the array (therefor it calls the default constructor maxBullets times). Now, if the above is true then I am guessing that my window / program is crashing because I set HWND and HDC to NULL in the default constructor for Bullet. If this is my problem, I am at a loss as to how I would resolve it, since I think it''s needed, or at least good, to have a default constructor and the only safe thing I can think of to default my HWND and HDC variables to is NULL. So... I hope that makes sense, I know it''s a much larger post then I need, but I hope I explained my problem sufficiently and that someone can help because I can''t really move forward without resolving this problem.
Advertisement
const std::vector bullets(max_bullets, Bullet(h, hdcBackBuffer,1));

or...

SpaceShip::SpaceShip(HWND h, HDC backBufferDC)// initialization list to set the value for the const var's   :    aMag(1.0f), vMax(15.0f), maxBullets(5), bulletIndex(0),        GameObject(h, backBufferDC, 3),        v(),        bullets(std::vector<Bullet>(maxBullets, Bullet(h, backBufferDC, 1))


Obviously, you will need to include vector and make your bullet array a std::vector

[edited by - flangazor on August 14, 2003 3:08:29 PM]
Ok, but do I have to change to a vector? Or is there a way to do this with an array? It feels like there should be a way to do it with an array.

This topic is closed to new replies.

Advertisement