user2904033
user2904033

Reputation: 131

Why is the overloaded == operator function saying that 2 parameters is too many

The MS Visual c++ 2010 compiler is telling me the operator== has too many parameters, but let's it pass if I only put one. What's going on here? I got the function right out of Stroustrup's book, I"m pretty sure he knows how to overload an operator in 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;
    //....


  };

Upvotes: 1

Views: 431

Answers (2)

John Kugelman
John Kugelman

Reputation: 361739

A two-argument operator overload goes outside of the class definition.

class Book
{
    ...
};

bool operator==(const Book& a, const Book & b)
{
    return a.Return_ISBN()==b.Return_ISBN();
}

Upvotes: 4

Fred Foo
Fred Foo

Reputation: 363627

You defined operator== as a method (member function), so it has an implicit this argument of type Book*. Either use that or lift the definition outside the class. Since it doesn't use any private members, I'd do the latter.

Upvotes: 12

Related Questions