【发布时间】:2021-05-23 18:49:20
【问题描述】:
我正在尝试重载类中的运算符,但不知道类中前向声明性函数(如运算符)的正确语法。
template <typename S, typename T, typename R>
class TRIPLE
{
public:
TRIPLE(S, T, R);
const TRIPLE &operator=(const TRIPLE &other); // ok
TRIPLE friend bool &operator> (const TRIPLE &t1, const TRIPLE &t2); // not ok
S getFirst();
T getSecond();
void setFirst(S);
void setSecond(T);
private:
S *first;
T *second;
R *third;
};
template <typename S, typename T, typename R>
TRIPLE<S, T, R>::TRIPLE(S x, T y, R z) : first(x), second(y), third(z) {};
template <typename S, typename T, typename R>
const TRIPLE<S, T, R> &TRIPLE<S, T, R>::operator=(const TRIPLE<S, T, R> &other){
// all good.
}
template <typename S, typename T, typename R>
const TRIPLE<S, T, R> &TRIPLE<S, T, R>::operator>(TRIPLE<S, T, R> &lhs, TRIPLE<S, T, R> &rhs){
// error: overloaded 'operator>' must be a binary operator (has 3 parameters)
}
我尝试了各种方法:
TRIPLE friend bool &operator> (const TRIPLE &t1, const TRIPLE &t2);
const TRIPLE bool &operator> (const TRIPLE &t1, const TRIPLE &t2);
const TRIPLE friend bool &operator> (const TRIPLE &t1, const TRIPLE &t2);
编辑:
bool operator>(const TRIPLE &right) const;
没有错误,但是...
template <typename S, typename T, typename R>
bool operator>(TRIPLE<S, T, R> &rhs){
return rhs; // just for test
}
错误:重载的 'operator>' 必须是二元运算符(有 1 个参数)
【问题讨论】:
标签: c++ operator-overloading forward-declaration