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" '
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" '
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&); ^
std
namespaceWhat 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);
}
In-class binary operator
s 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.