c++ - 从 QList 中删除一个项目会抛出错误

标签 c++ qt qt4

如何从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/

相关文章:

c++ - 如何使用 "trusty"图像在本地执行 C++ 程序的 Travis CI?

c++ - 在不相关的全等类之间进行转换

c++ - Qt 中的正则表达式

c++ - 逐行读取文件与读取整个文件时的性能

c++ - Qt中的空文件

c++ - 将属性表升级到 Visual Studio 2010

c++ - bjam 运行时链接=静态

qt - QTableView 中只有复选框的列

qt4 - 从 qt4.8 移植到 qt5.2 后出现运行时错误

qt4 - 如何使用 Phantomjs 截图而不模糊