腐败而不提及阵列
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],那么你的问题是你在应该只使用 <的时候使用了><=。的时候使用了>
相关文章:
- OpenMP阵列性能较差
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- 当我的阵列太大时出现分段错误
- 位阵列上的快速AND运算
- 阵列必须使用大括号封闭的初始器进行初始化
- 没有从阵列<float>到阵列<int>的可行转换
- C++动态安全 2D 交错阵列
- 将平面阵列重塑为复杂的特征类型
- 如何使用英特尔 PIN 捕获阵列的所有负载?
- 为什么我能够为阵列分配比计算机实际拥有的内存更多的内存
- 库特<<恩德尔;不适用于打印 2D 阵列
- 数组类 阵列的打印输出
- 从较小的阵列到较大的阵列的元素级转换
- 仅在大型阵列上出现合并排序分段错误
- 从阵列C++中删除重复项
- 阵列的大小在C++中是否灵活?
- 反转阵列中的正序列
- 具有可变尺寸的 C++ 2D 阵列
- 二维阵列的动态分配
- 重新增长阵列时出错:写入时缓冲区溢出