QList<T>::operator[]: "index out of range" 文件 /usr/include/arm-linux-gnueabihf/qt5/QtCore/qlist.h 中的 ASSERT 失败

ASSERT failure in QList<T>::operator[]: "index out of range" file /usr/include/arm-linux-gnueabihf/qt5/QtCore/qlist.h

我最近在做一个项目。当我尝试 运行 项目时,出现此错误:

/ASSERT failure in QList<T>::operator[]: "index out of range", file /usr/include/arm-linux-gnueabihf/qt5/QtCore/qlist.h, line 487
The program has unexpectedly finished.

我该如何追踪问题的根源?

我认为是添加这段代码导致了错误

startvaluexy = Client::straightxy;
qDebug() << "start value Received from server :" << startvaluexy;
QStringList xy = startvaluexy.split("|");

x = xy[2];

QString num1 = x;
int x = num1.toInt();

qDebug() << "start x value :" << x;

y = xy[3];

QString num2 = y;
int y = num2.toInt();

qDebug() << "start y value :" << y;

把这个x = xy[2];y = xy[3];取出来,运行就可以了

您必须使用 xy.size() 检查拆分返回的 xy 列表的大小

...不确定你的代码是什么意思,但我会写这样的东西

if(xySize >= 4){
     QString num1 = xy[2];
     QString num2 = xy[3];

     int x = num1.toInt();
     int y = num2.toInt();

     qDebug() << "start x value :" << x;
     qDebug() << "start y value :" << y;

     xstart = x;
     ystart = y;
}

这工作正常。

startvaluexy = EchoClient::straightxy;
qDebug() << "start xy value Received from server :" << startvaluexy;
QStringList xy = startvaluexy.split("|");
int xySize = xy.size();
qDebug() << "start xy size :" << xySize;

if(xySize < 4){
    return false;
}

bool ok;
int x = xy[2].toInt(&ok);
if(!ok){
    return false;
}

int y = xy[3].toInt(&ok);
if(!ok){
    return false;
}

return true;