I'll try to be a little more thorough than the answers so far:
(1)
bool operator <= (Mystery); //Line 2
Const correctness for the invocant. Operator <=
is not supposed to modify the instance it is called on and is supposed to indicate not doing so by the trailing const
modifier:
bool operator <= (Mystery) const;
Everybody else suggested that the right argument should be passed by const reference, but here it depends on what the class contains. Numbers are normally passed by value and if the Mystery
contains just one member of primitive type and has default or inline copy constructor, passing by value might be appropriate. When passing by value, the argument obviously can't be modified, so you don't need to care for const
. Applies to all further cases too. Using const reference
bool operator <= (Mystery const &) const;
is more common because most user classes do contain more than just one or two numbers and for the trivial cases everything is usually inline anyway and the compiler will optimize it as it sees fit.
If the class has any constructors suitable for type conversion (non-explicit single argument constructors from different types), the operator should be overloaded as non-member function (like the operator+
in the case (3)).
This is because a non-member operator<=
will be found if the arguments are merely convertible to Mystery
, while member operator will only be found if the left operator is Mystery
or derived type.
Overloaded operators are only considered if at least one argument is a user-defined type, i.e. the compiler will not consider bool operator<=(Mystery, Mystery)
on arguments of type (int, int)
even when there is a Mystery::Mystery(int)
constructor that would make it viable conversion. It will however consider non-member operator for argument list of (int, Mystery)
while it will not consider a member bool Mystery::operator<<(Mystery) const
operator for that.
bool Mystery::<=(Mystery rightObj) //Line 3
Missing operator
keyword.
bool Mystery::operator<=(Mystery rightObj)
(2)
bool operator <= (Mystery, Mystery); //Line 2
Incorrect signature. operator<=
takes 2 arguments, one of them is the invocant and it therefore needs just one explicit argument.
(3)
friend operator+ (Mystery); //Line 2
This refers to a non-member operator+
.
Since operator+
is also binary operator, it needs two arguments.
Operator +
needs a return type. The return type should usually be a new instance (i.e. return by value) of the argument class.
Non-member functions don't have the trailing const
modifier, because they don't have invocant. The arguments should be passed either by value or by const reference depending on the content of the class:
friend Mystery operator+ (Mystery, Mystery);
friend Mystery operator+ (Mystery const &, Mystery const &);
The friend
keyword only appears when declared inside the class to give the function access to it's private members. Outside the class (e.g. in definition) it is just:
Mystery operator+ (Mystery, Mystery);
Mystery operator+ (Mystery const &, Mystery const &);