腐败而不提及阵列

Corruption without mentioning array

本文关键字:阵列      更新时间:2023-10-16

我对 c++ 相当陌生,想知道为什么我的数组总是损坏。在损坏ammo_bank之前,它会多次通过双精度 for 循环,尽管它在该行之前被正确分配。然后它给了我错误编写违规

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++;
}

如何声明 screen[][]。 如果是 screen[SCREEN_HEIGHT][SCREEN_WIDTH],那么你的问题是你在应该只使用 <的时候使用了><=。