Let's start with the fact that you declared a constructor with one parameter
Complex (double re) { this->re = re; this->im = 0; };
However, in this statement you are trying to call a constructor with two arguments
Complex Complex::operator+ (Complex c) { Complex cc(c.re + re, c.im + im); ^^^^^^^^^^^^^^^^^^^^ return cc; };
Obviously, you must also declare a constructor with two parameters.
These operators
Complex operator+ (Complex c); Complex operator+ (double d);
better to declare as follows
Complex operator +( const Complex &c ) const; Complex operator +( double d ) const;
since 1) the objects that add up themselves do not change (therefore the const qualifier is added after the list of parameters); and 2) it is advisable to pass the parameter of type Complex by a constant reference to avoid unnecessary creation and copying of temporary objects.
This design
return (c+this*);
syntactically incorrect.
The class definition may look like this, as shown in the demo program below.
#include <iostream> class Complex { private: double re, im; public: explicit Complex ( double re = 0.0, double im = 0.0 ) { this->re = re; this->im = im; } Complex operator +( const Complex &c ) const; Complex operator +( double d ) const; friend std::ostream & operator <<( std::ostream &os, const Complex &c ) { return os << "{ " << c.re << ", " << c.im << " }"; } }; Complex Complex::operator +( const Complex &c ) const { return Complex( this->re + c.re, this->im + c.im ); } Complex Complex::operator +( double d ) const { return Complex( this->re + d, this->im + d ); } int main() { Complex c1( 10, 10 ); std::cout << "c1 = " << c1 << std::endl; Complex c2 = c1 + 5; std::cout << "c2 = " << c2 << std::endl; Complex c3 = c1 + c2; std::cout << "c3 = " << c3 << std::endl; return 0; }
Output of the program to the console
c1 = { 10, 10 } c2 = { 15, 15 } c3 = { 25, 25 }
Or the operator operator +( double ) const at your discretion, can be defined as
Complex Complex::operator +( double d ) const { return Complex( this->re + d, this->im ); }
As for the use of this in the returned expression from operators, then in these operators, which you have declared, there is little point in using this .
But you could define operator += , that is, a composite assignment operator. They may look like this.
Complex & operator +=( const Complex &c ) { this->re += c.re; this->im += c.im; return *this; } Complex & operator +=( double d ) { this->re += d; this->im += d; return *this; }
or
Complex & operator +=( double d ) { this->re += d; return *this; }
Here, in return values from operators, a pointer to this objects is used.
Another possibility to use this is to define operator operator +( double ) const through operator operator +( const Complex & ) const . In this case, the definition of operator operator +( double ) const can be as follows.
Complex Complex::operator +( double d ) const { return *this + Complex( d, d ); // return Complex( this->re + d, this->im + d ); }
or
Complex Complex::operator +( double d ) const { return *this + Complex( d ); // return Complex( this->re + d, this->im ); }