简体   繁体   English

C ++模板类编译错误

[英]C++ template class compilation error

I get the following 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&)¶

Although I have implemented the operator== overload as follows for my template class: 尽管我为模板类实现了operator ==重载,如下所示:

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

For my template class: 对于我的模板类:

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

Do you know what the problem might be? 您知道可能是什么问题吗? Many thanks. 非常感谢。

Your boolean operator is declared as non-const. 您的布尔运算符被声明为非常量。 Fix it as follows in case rt1 is a const reference. 如果rt1是const引用,请按以下所示进行修复。 Note the added const keyword. 请注意添加的const关键字。

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

Explanation: C++ has two basic syntaxes for overloading a comparison operator; 说明:C ++有两种用于重载比较运算符的基本语法: a member operator with one other argument, or a static operator with two arguments. 一个成员运算符带有另一个参数,或者是一个静态运算符带有两个参数。 However, in both cases, you should make sure that both operands are const , with the respective syntaxes. 但是,在两种情况下,都应确保两个操作数均为const ,并具有各自的语法。

It is theoretically possible to supply different const and non- const versions of the operator that do subtly different things, so the compiler calls yours a near-match, but nevertheless not a match. 从理论上讲,可以提供不同的const版本和非const版本的运算符来完成不同的事情,因此,编译器将您的匹配结果称为“接近匹配”,但仍不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM