0

Why does the following code

class Foo {
public:
    bool std::operator==(const Foo&, const Foo&);
};

comes up as an error ' qualifier must be a base class of "Foo" '

2 Answers2

2

I get a different (maybe more reasonable) error message here:

main.cpp:4:48: error: invalid use of '::'
     bool std::operator==(const Foo&, const Foo&);
                                            ^ 
  1. You cannot arbitrarily overload binary operator function as class member operator functions (see Operator Overloading).
  2. You cannot do this referencing the std namespace

What you probably wanted is

class Foo {
public:
    bool operator==(const Foo&) const;
};

If you're really sure you want to overload this in the std:: namespace, you can write:

namespace std {
    bool operator==(const Foo& op1, const Foo& op2) {
        return op1.operator==(op2);
    }
} 

or simply overload the binary operator==() function in the global namespace:

bool operator==(const Foo& op1, const Foo& op2) {
    return op1.operator==(op2);
}
Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • ty, the std was an accident and on reflection idk why it was there, that error message is also a lot clearer ty –  Apr 19 '16 at 16:39
0

In-class binary operators have an implicit Foo const & as the left-hand side. Thus, you should only include one Foo const & as a parameter, not two.

edit: also, as mentioned in the other answer, you would have to drop the std:: in this case too.

underscore_d
  • 6,309
  • 3
  • 38
  • 64