没有提到阵列的腐败

时间:2012-08-13 20:44:52

标签: c++ visual-studio-2010

我对c ++很新,我想知道为什么我的数组不断被破坏。它在破坏ammo_bank之前经过了几次双循环,尽管它在该行之前被正确分配。然后它给我写错误

class bullet{
public:
    int x, y, damage, speed;
    char direction;
};

bullet * ammo_bank[100];
void render(player avatar, riflemen enemy){
    bullet projectile;
    int counter1, counter2, icurrentammo;
    icurrentammo = current_ammo -1;
    for (counter1 = 0; counter1 <=SCREEN_HEIGHT; counter1++){
        for (counter2 = 0; counter2 <=SCREEN_WIDTH; counter2++){     // corruption occurs a few times before here
            screen[counter1][counter2] = '.';
        }
    }
    system("cls");
    screen [avatar.y][avatar.x] = AVATAR_SYMBOL;
    screen [enemy.y][enemy.x] = RIFLEMEN_SYMBOL;
    while (icurrentammo >= 0){
        projectile = *ammo_bank[icurrentammo];                 // Writing error
        screen[projectile.x][projectile.y] = BULLET_SYMBOL;
        projectile.x ++;
        icurrentammo --;
    }

    for (counter1 = 0; counter1 <=SCREEN_HEIGHT; counter1++){
        cout << endl;
        for (counter2 = 0; counter2 <=SCREEN_WIDTH; counter2++){
            cout << screen[counter1][counter2];
        }
    }


void playerShoot(player avatar){
    ammo_bank[current_ammo] = new bullet(); // Create the MyClass here.
    bullet projectile = *ammo_bank[current_ammo];
    projectile.x = avatar.x + 1;
    projectile.y = avatar.y;
    projectile.speed = 2;
    projectile.direction = 'f';
    projectile.damage = 1;
    *ammo_bank[current_ammo] = projectile;
    current_ammo++;
}

1 个答案:

答案 0 :(得分:2)

如何声明screen [] []。如果它是屏幕[SCREEN_HEIGHT] [SCREEN_WIDTH],那么你的问题是你正在使用&lt; =当你应该使用&lt;。