2012-10-15 46 views
0

我得到以下编译错误:C++模板类的编译错误

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&)¶ 

虽然我已经实现了==操作符重载我的模板类,如下所示:

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 

你知道这个问题可能是什么吗?非常感谢。

+0

什么是'特殊'类? – interjay

+1

'rt1'和'rt2'的类型是什么? – ecatmur

回答

1

您的布尔运算符被声明为非const。如果rt1是一个常量引用,请按以下方式进行修复。请注意添加的const关键字。

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

说明:C++有两个基本语法用于重载比较运算符;具有一个其他参数的成员运算符或具有两个参数的静态运算符。但是,在这两种情况下,都应该确保两个操作数都是const,并使用相应的语法。

理论上可以提供不同的const和非const版本的操作符,这些操作符做了细微的不同的事情,所以编译器会调用你的近似匹配,但不匹配。