Binary operators don't attempt any conversion to the left side argument, the "receiver" of the function.
If you want a builtin type on the left hand side, the overload must be a free function.
The simplest way is usually to have a mutating member, +=
in this case, and delegate to that from the free function(s).
Something like this:
complex complex::operator+=(complex rhs)
{
re += rhs.re;
im += rhs.im;
return *this;
}
complex operator+(complex t, complex c)
{
t += c;
return t;
}
int main()
{
complex z(2.0);
complex w(3.0, 4.0);
printf("%f\n",(2.0 + z).getre());
}
You should also make your getters const
.
And you receive a gold star for not having setters.