Итак, я пишу простую систему битв для игры, и я получаю ошибку, передавая массив указателей на класс битвы.Ошибка ошибочного указателя в классе при передаче массива
//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);
Так что это работает нормально, но когда я передать массив в классе, первый член проходит нормально, но остальные пропускаются, и когда я делаю контрольную точку, они посылаются как «Badptr».
Вот код для конструктора battlesystem:
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;
}
я что-то очевидное, я думаю, что не хватает, но может кто-нибудь поделиться мудростью?
спасибо.
жизни намного проще с помощью стандартных контейнеров, таких как 'станд :: VECTOR' и 'std :: array', попробуйте изменить свой код, чтобы использовать их, если сможете. Также вы можете включить декларации переменных члена класса в свой вопрос. – shuttle87
'BattleSystem * btl = new BattleSystem (& player, * foes, 3);' <- Вы * разыгрываете * 'противники, что не имеет никакого смысла. – crashmstr
Вы уверены, что игрок не выйдет из сферы действия? – drescherjm