Override operator overloading in C ++?

helo guys

I have a call Complex class

I have overloaded the operator, for example

Complex c = a + b; // where a and b are object of Complex class 

      

which is basically the + (Complex & that) operator;

but i don't know how to say, for example

double c = a + 10; //where a is object of Complex class but 10 is integer / double  

      

I defined typecasting to be double so that my IDE says there are too many operands + and it somehow complains about not being able to "understand" +

it should be in this format though double c = a + 10;

thanks

error message

Error: more than one operator "+" matches these operands: 
error C2666: 'Rational::operator +' : 3 overloads have similar conversions 

1> could be 'const Complex Complex::operator +(const Complex &)' 1> 
or 'double operator +(const Complex &,double)' 

      

compiler can't select based on signature? and yes i defined it outside the class because i had one defined inside the class thanks

+2


a source to share


5 answers


double operator+(const Complex &c, int x)
{
    //....
}

      



+4


a source


How to add to form constructor:

 Complex(float _real) : m_Real( _real ), m_Imaginary(0){}

      



so any meaning that can be added to float

can be perceived as Complex

. Then you don't need to do the overloaded operator for all types. The one you wrote for Complex

will suffice.

+3


a source


The reason you are getting an ambiguous overload error is because you have options operator+

that can add two Complex

or Complex

and double

, but you are trying to add Complex

and int

. The compiler cannot decide whether it is better to convert int

to Complex

to use the former or double

and use the latter.

To avoid this, you need to either define an overloaded operator+

for all possible types that you can add to Complex

(int, float, long, unsigned ...) OR not overload operator+

in the first place - just define SINGLE operator+

, which adds two Complex

and allows type conversions with all other cases.

+2


a source


Overloading operator+

for double operand:

double Complex::operator+(double rhs)

      

+1


a source


If you are planning to do this, you can do the following. First, we define the following (outside of the class to allow implicit conversion for both the first and second operand), DO NOT define any other + operator, such as the + operator (complex, double):

Complex operator+(const Complex& a, const Complex& b) {
  // ..
}

      

At the same time, define an implicit constructor:

Complex(double a) : real(a), imag(0) {}

      

And then define a conversion operator (as wheat pointed out, this can be considered bad programming practice and I agree, so if no final conversion to double is required, omit that):

operator double() const { return real; }

      

This will automatically support double c = a_complex + 10;

and double c = 10 + a_complex;

, the number 10

will be implicitly converted to Complex

using the implicit constructor, and arithmetic will be resolved to operator+(const Complex&, const Complex&);

, the result will be automatically converted to double.

PS You can also define Complex& operator+=(const Complex& o) { /* .. */ }

inside a class and use it to implement operator+

above.

0


a source







All Articles