如何从Qlist中删除对象
QList<CascadeJobInfo> m_InternJobInfoList;
foreach (CascadeJobInfo jobInfo, m_InternJobInfoList)
{
m_InternJobInfoList.removeOne(jobInfo);
}
它抛出错误 C:\Qt\Qt5.7.0\5.7\mingw53_32\include\QtCore\qlist.h:972: 错误:'operator=='不匹配(操作数类型是'CascadeJobInfo'和'const CascadeJobInfo') 如果 (n->t() == t) ^
最佳答案
您需要为 CascadeJobInfo
类型实现 operator==
:
class CascadeJobInfo
{
public:
<...>
bool operator==(const CascadeJobInfo & other) const;
<...>
};
bool CascadeJobInfo::operator==(const CascadeJobInfo & other) const
{
if (this == &other) {
return true;
}
bool equal = <...compare each data member within this object with its counterpart in other...>;
return equal;
}
official documentation说的很清楚:
This function requires the value type to have an implementation of operator==().
此外,您还不清楚您要从您的代码片段中实现什么。尝试在遍历列表时删除每个列表的项目有一个更简单的替代方法:方法 clear() .
关于c++ - 从 QList 中删除一个项目会抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46241140/