Overloaded ‘operator>>’ must be a binary operator (has 3 parameters)
解决方法
遇到这个问题一般是在类内编写重载运算符的时候遇到。
解决方式有两种:
- 如果你希望其变为一个友元函数,那么直接在重载运算符的声明前面加上friend关键字
friend std::istream& operator>> (std::istream&, Str&);
- 如果你不希望其变为一个友元函数使得可以访问类内成员,那么就将其在类的外部进行声明
class class_name{
}
std::ostream& operator<< (std::ostream&, const Str&);
问题分析
这个bug潜在的原因为:
A member function takes an implicit first parameter, meaning your operator now takes three parameters
一个成员函数需要隐式的获取第一个参数,这就导致在类内编写重载二元运算符的成员函数的时候需要传递三个参数,从而导致了这个bug。