我在这里看到了其他问题,但它们处理指针或qobject派生类,因此它们似乎不相关。
我试图将值附加到自定义类的qlist中,下面是类
class matchPair
{
public:
matchPair(int a=0, int b=0)
: m_a(a)
, m_b(b)
{}
int a() const { return m_a; }
int b() const { return m_b; }
bool operator<(const matchPair &rhs) const { return m_a < rhs.a(); }
// matchPair& operator=(const matchPair& other) const;
private:
int m_a;
int m_b;
};
class videodup
{
public:
videodup(QString vid = "", int m_a = 0, int m_b = 0);
~videodup() {}
QString video;
bool operator==(const QString &str) const { return video == str; }
// videodup& operator=(QString vid, int m_a, int m_b);
QList<matchPair> matches;
};
struct frm
{
QString file;
int position;
cv::Mat descriptors;
QList<videodup> videomatches;
};
QList<frm> frames;
失败的原因是:
frame.videomatches.at( frame.videomatches.indexOf(vid) ).matches.append(pair);
我得到的错误是:
/usr/local/Cellar/qt5/5.5.1_2/lib/QtCore.framework/Headers/qlist.h:191: candidate function not viable: 'this' argument has type 'const QList<matchPair>', but method is not marked const
void append(const T &t);
^
我做错什么了?
发布于 2016-02-07 02:19:30
您正在尝试将一个值附加到const QList<T>
,这意味着您的QList<T>
是常量的,即不可变。仔细看,错误读取this has type const QList<matchPair>
,您只能在const
对象上调用const
方法,而append()
显然在语法和语义上都不是const
。将QList<matchPair>
修正为不为const
。
编辑2:
仔细看过代码之后,这就是罪魁祸首,确实是:
frame.videomatches.at( frame.videomatches.indexOf(vid) ).matches.append(pair);
^^
QList<T>::at()
返回const T&
,这导致了我前面描述的问题。请使用QList<T>::operator[]()
insdead,它具有返回const T
和T
值的重载。
编辑:
然而,这是哪个编译器品牌和版本呢?我不能在g++中通过调用const
类对象上的一个非const
方法来再现这个错误消息,无论是模板对象还是非模板对象(我得到了一个错误,但措辞不同)。
https://stackoverflow.com/questions/35248930
复制相似问题