簡體   English   中英

C ++模板類編譯錯誤

[英]C++ template class compilation error

我收到以下編譯錯誤:

main.cc: In function 'int main(int, char**)':¶
main.cc:200: error: no match for 'operator==' in 'rt1 == rt2'¶
triple.hh:124: note: candidates are: bool Triple<T1, T2, T3>::operator==(const    Triple<T1,T2, T3>&) [with T1 = int, T2 = int, T3 = int] <near match>¶
main.cc:27: note:                 bool operator==(const Special&, const Special&)¶

盡管我為模板類實現了operator ==重載,如下所示:

bool operator==(const Triple<T1, T2, T3>& another) {
    return (a == another.first() and b == another.second() and c == another.third());
}

對於我的模板類:

template <typename T1, typename T2, typename T3>
class Triple

您知道可能是什么問題嗎? 非常感謝。

您的布爾運算符被聲明為非常量。 如果rt1是const引用,請按以下所示進行修復。 請注意添加的const關鍵字。

bool operator==(const Triple<T1, T2, T3>& another) const {

說明:C ++有兩種用於重載比較運算符的基本語法: 一個成員運算符帶有另一個參數,或者是一個靜態運算符帶有兩個參數。 但是,在兩種情況下,都應確保兩個操作數均為const ,並具有各自的語法。

從理論上講,可以提供不同的const版本和非const版本的運算符來完成不同的事情,因此,編譯器將您的匹配結果稱為“接近匹配”,但仍不匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM