线程1:EXC_BAD_ACCESS错误,此错误在第一行的CPP文件中的句柄函数中出现
THREAD 1 :EXC_BAD_ACCESS error, this error appears at the handle function in the cpp file at the first line
这是一种创建鸡肉入侵者游戏的代码,游戏(或我可以做的任何部分(在我初始化一只鸡时工作正常,但是当我制作它时一批鸡阵列向我展示了这个错误。
RenderWindow window;
int main(int, char const**)
{
chicken chick[4][7];
Sprite back;
Clock clock;
window.create(VideoMode(1800, 1000), "Menu");
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::KeyPressed && event.key.code == Keyboard::Escape)
window.close();
if (event.type == Event::Closed)
window.close();
}
Texture texture;
if (!texture.loadFromFile(resourcePath() + "background.jpg"))
{
std::cout << "Error loading texture" << std::endl;
}
Time time;
time = clock.getElapsedTime();
if (time.asSeconds() >= 0.001)
{
chick[4][7].handle(window, clock);
clock.restart();
}
window.clear();
back.setTexture(texture);
back.setPosition(0, 0);
back.setTextureRect(IntRect(0, 0, 1800, 1000));
window.draw(back);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 7; j++)
chick[i][j].initialize(window);
window.display();
}
}
///////////////////
this is the app file for the chicken class
using namespace sf;
class chicken
{
private:
RectangleShape chick;
bool flag;
public:
chicken();
void initialize(RenderWindow &window);
void handle(RenderWindow & window, Clock clock);
~chicken();
};
#endif /* chicken_hpp */
/////////////////////////////////////////////////////////////
this is the cpp file for the chicken class
using namespace sf;
chicken::chicken()
{
chick.setPosition(500, 500);
chick.setSize(Vector2f(200, 200));
chick.setOrigin(100, 100);
flag = true;
}
void chicken::initialize(RenderWindow & window)
{
Texture texture2;
if (!texture2.loadFromFile(resourcePath() + "chicken3.jpg"))
{
std::cout << "Error loading texture" << std::endl;
}
chick.setTexture(&texture2);
window.draw(chick);
}
void chicken::handle(RenderWindow & window, Clock clock)
{
if (flag) //the error appears here
{
chick.move( 10 , 0 ) ;
}
else
{
chick.move(-10 , 0);
}
if (chick.getPosition().x >= window.getSize().x)
flag = false;
if(chick.getPosition().x < 0)
flag = true;
}
chicken::~chicken()
{
}
将来请在问题中包含整个错误消息,但是这里的问题是您正在访问鸡肉阵列中的界限索引。
您的数组大小为4x7,这意味着您的有效索引为0-3和0-6。
您的手柄功能在小鸡[4] [7]上被调用。
如果您只想在数组中的最后一只小鸡上调用该功能,那将是
chick[3][6].handle(window, clock);
要在每只鸡上打电话给您需要双循环的双重
for(int i = 0; i < 4; ++i)
{
for(int j = 0; j < 7; ++j)
{
chick[i][j].handle(window, clock);
}
}
相关文章:
- 课堂上的一行,使整个应用程序坏了. 0xC000005错误
- 从带有 getline() 的文件读入一行上有多个信息得到无效参数错误
- 线程1:EXC_BAD_ACCESS错误,此错误在第一行的CPP文件中的句柄函数中出现
- C++在 while 循环后添加一行代码会导致错误
- 使用ifstreamwhile循环,如何显示输入错误并在下一行继续
- clang format函数和错误检查在同一行
- 用“ strcpy”在2D数组中删除(字符)的一行(字符)时是否存在任何错误
- 最后一行执行后的堆栈错误
- 错误 C2144 和错误 C4430 在同一行上
- 如果语句,可能是错误的,输入后不要移至下一行
- 在Visual Studio中重建我的项目时,我会在同一行错误C2371上遇到2个错误
- scanf() 为最后一行输入提供错误的输出
- 链接器不指出错误;多个定义警告指向同一行
- 如何一次一个单词而不是一行一行地消除错误输入
- c++计算器atof没有错误,但它在中间一行崩溃(52)
- GCC:为什么一行代码中的错误(字符串和NULL的比较)会导致一长串错误消息
- 根据关键字拆分一行(有拼写错误)
- Makefiles:是否有一种通用的方法来显示makefile中的哪一行导致了错误
- 简单的一行类方法返回错误的值
- 编译错误,注释/debug/uncomment/debug在一行上消失