I have the following code:

class Complex { private: double re, im; public: Complex (double re) { this->re = re; this->im = 0; }; Complex operator+ (Complex c); Complex operator+ (double d); }; Complex Complex::operator+ (Complex c) { Complex cc(c.re + re, c.im + im); return cc; }; Complex Complex::operator+ (double d) { Complex c(d); return (c+this*); }; 

I get the error:

 expected expression return (c+this*); 

Why?

    3 answers 3

    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 ); } 
    • But should the addition of double to Complex change the imaginary part of a number? - PetSerAl
    • @PetSerAl This is how you decide. Both approaches look logical. - Vlad from Moscow
    • one
      In mathematics, the real number d is usually associated with a complex number (d, 0) , so mathematicians would expect the same result as adding d + (re, im) as they do from addition (d, 0) + (re, im) . - VladD
    • @VladD In this question, not mathematics is discussed, but how correctly from the point of view of the C ++ language to write the operators, how to declare them, and what to return from them. Inside the body of operators, you can put any calculations that you consider necessary. - Vlad from Moscow
    • @VladfromMoscow: I understand that, of course. Therefore, I have a subjunctive mood in the commentary. - VladD

    An asterisk for dereferencing this is placed on the other side:

     Complex Complex::operator+ (double d) { Complex c(d); return (c + *this); } 

    By the way, your code lacks a constructor with two double parameters:

     Complex (double re, double im) : re(re), im(im) { } 

    A constructor with a single parameter is usually also written in a simplified form:

     Complex (double re) : re(re), im(0) { } 
    • And what about this kind of "simplified"? - AnT
    • @AnT: missing = and this ? - VladD
    • @AnT: Yes, I know that calling a constructor and assigning it is, formally speaking, not the same thing. But I do not want to confuse the vehicle, yet the level of the task is quite new. - VladD

    The pointer dereference operator in C ++ is a unary operator. And like any unary operator, it is indicated before its operand, and not after it: *this .

    Generally speaking, if your constructor from double type is not explicit , then write a separate operator for double type

     Complex operator+ (double d); 

    formally there is no need. The compiler will automatically interpret the expression Complex + double as Complex + Complex(double) , i.e. will use the already existing + operator.

    However, it is worth noting that such + operators implemented through member functions of a class must be declared const

     Complex operator+ (Complex c) const; 

    and it is probably better to pass the parameter by a constant link.

    It is also worth noting that it would be necessary to implement these operators with stand-alone functions, and not as member functions of the class. Because you have made these functions members of a class, the Complex + double expressions are supported by you, but the double + Complex are not.