0

novice programmer here, I'm using c++ with the SFML library, Code::Blocks, and the GNU GCC Compiler. Windows XP.

I'm making a simple platformer as practice for a larger game in the future. I decided to set it up so that it reads all of the level data from a text file. I just added enemies to the program (although right now it just prints a sprite on screen) and it worked fine the first time, but after that it crashes during runtime. Here's the relevant bits of code:

Enemy.h:

class Enemy
{
    ...
}

typedef Enemy* EnemyPointer;

Main.h:

class App
{
    private:
        sf::Image EnemyImage;

        EnemyPointer TheEnemies;

        int LevelData[256][4];

    public:
        App();

        int Execute();

        bool Init();

        void Cleanup();
}

Main.cpp:

App::App()
{
    TheEnemies = new Enemy[16]
}

int main()
{
    App TheApp;
    return TheApp.Execute();
}

int App::Execute()
{
    if(!Init()) return -1;

    LoadLevel();

    while(Running)
    {
        OnEvent(TheEvent);

        Loop();

        Render();

        Frame++;
    }

    Cleanup();

    return 0;
}

Init.cpp:

bool App::Init()
{
    if(!EnemyImage.LoadFromFile("Enemy.bmp"))
    {
        std::cout<<"Failed to load \"Enemy.bmp\"";
        return false;
    }

    //Program crashes in this for loop.

    for(int c=0;c<=LevelData[0][1];c++)
    {
        TheEnemies[c].SetX(LevelData[LevelData[0][0]+1+c][0]);
        TheEnemies[c].SetY(LevelData[LevelData[0][0]+1+c][1]);
        TheEnemies[c].SetType(LevelData[LevelData[0][0]+1+c][2]);

        TheEnemies[c].Sprite.SetImage(EnemyImage);
    }
}

Cleanup.cpp:

void App::Cleanup()
{
    delete[] TheEnemies;
}

LevelData[][] is a 2D array containing the information that's read from the .txt file. It contains the number of enemies, their coordinates, and their types.

I've determined that the program crashed during Init() when it tries to use the array of Enemies that was created in the constructor. I marked it with a comment. I've looked up forum posts about how to create an array of objects and most of them say this is how to do it. I can't figure out why it's crashing. If anyone know how to fix this it would be greatly appreciated. Thanks.

phs
  • 10,378
  • 3
  • 56
  • 80
Jonathan
  • 1
  • 1
  • Why on earth would you not use the destructor? That's exactly what it's for! Also, you need to follow the [rule of three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – chris Jun 22 '12 at 04:24
  • I'm speechless that I didn't think of that... sorry this is pretty much the first time I've used classes in a real project so I'm not used to them. I've never had to use a destructor. – Jonathan Jun 22 '12 at 04:37
  • Okay I moved delete[] TheEnemies; to the destructor but I'm still getting the same problem. (I'm reading about the rule of three, I've never heard of it) – Jonathan Jun 22 '12 at 04:39
  • Well, you're only ever using one instance here, but it's always a good idea to follow it when your class has ownership of a resource. – chris Jun 22 '12 at 04:43
  • So that doesn't have anything to do with the crashing problem? – Jonathan Jun 22 '12 at 04:49
  • `LevelData[][] is a 2D array containing the information that's read from the .txt file` Where in the code do you initialize it? As far as I can tell, `LevelData` contains nothing. – Jesse Good Jun 22 '12 at 04:50
  • @Jonathan, It probably doesn't, but now you know what you should be including before you run into some more errors later. – chris Jun 22 '12 at 04:52
  • Sorry no I didn't include that in the code. It does contain information, it reads the file during LoadLevel(). – Jonathan Jun 22 '12 at 05:00
  • Jesus I'm dumb. I solved the problem, I was using values from LevelData in Init() when I didn't actually put the values into it until after Init(). This made it try to use an empty variable and crash. Thanks for the destructor tip though! – Jonathan Jun 22 '12 at 05:04
  • Create an answere by your own and accept it to close this question please – Sebastian Hoffmann Jun 22 '12 at 06:19

0 Answers0