我宣布我的头文件中像这样的数组:C++ Segfault,我不知道为什么?
private:
int frames[10];
而且在类的构造函数分配了类似这样的值:
file.open(File);
if(file.is_open())
{
std::string line;
getline(file, line);
std::string param[10];
std::stringstream stream(line);
int n=0;
while(!stream.eof())
{
getline(stream, param[n], '$');
frames[n] = atoi(param[n].c_str());
n++;
}
file.close();
}
这个阵列上后来在一个函数:
currentFrame++;
if(frames[currentAnimation] <= currentFrame)
{
currentFrame = 0;
}
当我运行我运行我的代码我得到分段错误,gdb返回此:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000402c22 in Sprite::update (this=0x7ffff6efe678 <main_arena+88>) at Sprite.cpp:93 93
if(frames[currentAnimation] <= currentFrame)
(gdb) bt
#0 0x0000000000402c22 in Sprite::update (this=0x7ffff6efe678 <main_arena+88>) at Sprite.cpp:93
#1 0x0000000000401fcb in main (argc=1, argv=0x7fffffffeb88) at main.cpp:146
我不确定我要去哪里错,我认为这个错误在这里。 我真的不能发布所有的代码作为它的很多,但如果你需要更多 具体信息请直接询问。
非常感谢您提前。
你如何确保'ñ<10'或'currentAnimation <10'?最有可能你被界访问了调用未定义的行为。 –
我猜框架[currentAnimation]引用数组外 – Ashalynd
你试过打印出你使用的索引吗? –