2016-02-07 127 views
1

我在这里看到过其他问题,但它们处理指针或与qobject派生类,所以它们似乎不相关。qlist自定义类无法追加

我试图值追加到自定义类的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); 
     ^

我做错了什么?

+0

现在你还没有指定'frame'的类型以及它是如何被创建的。有一个'const'的地方,这是肯定的。 – iksemyonov

+0

对于未来的读者,我上面的评论是错误的,诀窍在'QList :: at()'方法中。所提供的信息足以解决问题。 – iksemyonov

回答

2

您正试图在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 TT的超载值。

编辑:

但是,哪个编译器的品牌和版本是这样的?我无法在g ++中通过调用const类对象的非const方法来重现此错误消息,这两个方法都是模板化的和非模板化的(出现错误,但措辞不同)。

+0

qt creator 3.6.0 qt5.5 – Neal

+0

好的,但这是Qt库版本,而不是编译器本身! – iksemyonov

+0

gcc配置:--prefix =/Applications/Xcode.app /目录/ Developer/usr --with-gxx-include-dir =/usr/include/C++/4.2.1 Apple LLVM版本7.0.2 -700.1.81) 目标:x86_64-apple-darwin15.3.0 – Neal