Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 我的程序在Windows上看起来不错,而在Ubuntu上它崩溃了_Qt_Qt5 - Fatal编程技术网

Qt 我的程序在Windows上看起来不错,而在Ubuntu上它崩溃了

Qt 我的程序在Windows上看起来不错,而在Ubuntu上它崩溃了,qt,qt5,Qt,Qt5,我正在用QT在Windows上编写一个飞机游戏,看起来不错,但在Ubuntu上它崩溃了。它有一个QLinkedList Bullet\u list。它在Bullet\u list之后崩溃。removeOne(*ite) 我尝试过判断每个空指针,在removeOne()之后添加delete*ite,但没有效果 Plane\u Player*Plane\p connect(timer,SIGNAL(timeout()),this,SLOT(Refresh())); void GameWindow::

我正在用QT在Windows上编写一个飞机游戏,看起来不错,但在Ubuntu上它崩溃了。它有一个
QLinkedList Bullet\u list
。它在
Bullet\u list之后崩溃。removeOne(*ite)

我尝试过判断每个空指针,在
removeOne()
之后添加
delete*ite
,但没有效果

Plane\u Player*Plane\p

connect(timer,SIGNAL(timeout()),this,SLOT(Refresh()));
void GameWindow::Refresh(){
QLinkedList<Bullet*>::iterator ite;
       for(ite = Plane_p->Bullet_list.begin();ite != Plane_p->Bullet_list.end();++ite)
       {
           (*ite)->Go();//move the bullets on the screen
           if((*ite)->isOutOfBound())
               Plane_p->Bullet_list.removeOne(*ite);//crashed after this.
       }
}

class Plane_Player
{
public:
    QLinkedList<Bullet *> Bullet_list;
}
connect(计时器、信号(timeout())、此、插槽(Refresh());
void GameWindow::Refresh(){
QLinkedList::迭代器ite;
对于(ite=Plane\u p->Bullet\u list.begin();ite!=Plane\u p->Bullet\u list.end();+ite)
{
(*ite)->Go();//移动屏幕上的项目符号
如果((*ite)->isOutOfBound())
Plane_p->Bullet_list.removeOne(*ite);//在此之后坠毁。
}
}
职业飞机手
{
公众:
QLinkedList项目符号列表;
}
在函数
Refresh()
中,有另一个
removeOne()
,之后它就崩溃了

我希望它不会在Ubuntu上崩溃。 整个项目:

说明:

如果要插入、修改或删除中间的项 列表中,必须使用迭代器

到目前为止还不错。但我认为问题在于,在从链表中删除一个元素后,需要重用迭代器。要修复它,您需要在删除后更新迭代器。例如,您可以重写循环,如下所示:

QLinkedList<Bullet*>::iterator ite = Plane_p->Bullet_list.begin();
while (ite != Plane_p->Bullet_list.end())
{
  (*ite)->Go();
  if ((*ite)->isOutOfBound())
  {
    ite = Plane_p->Bullet_list.erase(ite);
  }
  else
  {
    ++ite;
  }
}
QLinkedList::迭代器ite=Plane\u p->Bullet\u list.begin();
而(ite!=Plane\u p->Bullet\u list.end())
{
(*ite)->Go();
如果((*ite)->isOutOfBound())
{
ite=平面->项目符号列表。擦除(ite);
}
其他的
{
++白云石;
}
}
最后,它在不同平台上的工作方式不同,因为您的代码会导致未定义的行为