0

So I'm writing a simple battlesystem for a game and I'm getting an error passing an array of pointers to the battlesystem class.

//Create the player and 3 enemies
Battler player("Player", 100, 100, 50, 50, 50, 50, 90);
Battler foe1("Imp", 100, 100, 50, 50, 50, 50, 80);
Battler foe2("Ogre", 100, 100, 50, 50, 50, 50, 75);
Battler foe3("Giant", 100, 100, 50, 50, 50, 50, 60);

//Create an array of pointers that point to the enemies
Battler *foes[3];
foes[0] = &foe1;
foes[1] = &foe2;
foes[2] = &foe3;

//Initialize the battlesystem passing the player, the array of enemies 
//and the number of enemies (3)
BattleSystem *btl = new BattleSystem(&player, *foes, 3);

So this was working fine, but when I pass the array to the class, the first member is passed fine, but the rest are passed and when I do a breakpoint, they are sent as "Badptr".

Here is the code for the battlesystem constructor:

BattleSystem::BattleSystem(Battler *plyr, Battler enemies[], int numEnemies)
{
    player = plyr;

    //foe is declared as    Battler *foe;   So it just points to the first member of the enemies
    // array so I can access them. But only the first member gets a value the rest get
    // "Bad ptr" with garbage values and when I look through the enemies array passed
    // to the constructor, it has BAD PTRs in everything but the first element.
    foe = enemies;
    numFoes = numEnemies;

    totalTurns = 0;

    foeTurns = new int[numFoes];
    turnList = new Battler*[numFoes + 1];

    for(int i = 0; i <= numFoes; i++)
    {
        turnList[i] = &foe[i];
    }

    turnList[numFoes + 1] = player1;

}

I'm missing something obvious I think, but can anyone share some wisdom?

Thank you.

4
  • 2
    Life is so much easier with the standard containers such as std::vector and std::array, try changing your code to use those if you can. Also can you include the class member variable declarations in your question. Commented Dec 1, 2014 at 14:17
  • 2
    BattleSystem *btl = new BattleSystem(&player, *foes, 3); <- You are dereferencing foes, which does not make any sense. Commented Dec 1, 2014 at 14:18
  • Are you making sure that player will not go out of scope? Commented Dec 1, 2014 at 14:20
  • I would have went with the standard containers, but this code is ultimately going to end up crunched down into C for a device that containers are too costly on. Commented Dec 1, 2014 at 14:45

1 Answer 1

1

Leaving aside style issues about naked pointers and ownership, I believe you mean

//                                                v-- array of pointers
BattleSystem::BattleSystem(Battler *plyr, Battler *enemies[], int numEnemies)

And

BattleSystem *btl = new BattleSystem(&player, foes, 3);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.