operator+
should return an instance, not a reference:
// as member function
A operator+(const A& other);
// as free function
A operator+(const A& left, const A& right);
To explain the specific problem is "returns a constant reference which is then passed to const A& operator+( int m )
". Since you have a const reference, it cannot call that function because it's not a const method (i.e. const A& operator+( int m ) const
).
That said, that is not the way to fix operator+
. If you're returning a reference, what is it a reference to? A local in operator+ would be bad as you shouldn't return a reference to a local. A reference to a global would be bad because it will limit how your code can be used properly. A reference to allocated memory would be bad because it will leak memory. A reference to *this
would be bad because then operator+
is acting like operator +=
.