-1

私はC++にかなり慣れていないので、なぜ配列が破損し続けるのか疑問に思っています。その行の前に適切に割り当てられていますが、ammo_bankが破損する前に、doubleforループを数回通過します。その後、エラー書き込み違反が発生します

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

1 に答える 1

2

screen[][]はどのように宣言されますか。screen [SCREEN_HEIGHT] [SCREEN_WIDTH]の場合、問題は、<を使用する必要があるときに<=を使用していることです。

于 2012-08-13T20:48:57.613 に答える