MS Visual c++ 2010 编译器告诉我运算符 == 的参数太多,但是 如果我只放一个就让它过去吧。这里发生了什么?我从 Stroustrup 的书中得到了这个函数,我很确定他知道如何在 C++ 中重载运算符。
class Book
{
public:
Book(){}
Book(long long isbn,string ttl, string athr, int cpyrght_dt)
:ISBN(isbn), title(ttl), author(athr), copyright_date(cpyrght_dt) {}
//...
const long long & Return_ISBN () const {return ISBN; }
bool operator==(const Book& a, const Book & b)
{
return a.Return_ISBN()==b.Return_ISBN();
}
private:
long long ISBN;
//....
};
最佳答案
您将operator==
定义为一个方法(成员函数),因此它有一个Book*
类型的隐式this
参数。要么使用它,要么将定义提升到类之外。因为它不使用任何私有(private)成员,所以我会使用后者。
关于c++ - 为什么重载的 == 运算符函数说 2 个参数太多了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20052886/