2016-02-07 129 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