开发者

override the operator overloading in C++?

helo guys

i have class call Complex

I did operator overloading like such

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

which basically is operator+(Complex& that);

but I dont know how to say for example

开发者_开发技巧
double c = a + 10; //where a is object of Complex class but 10 is integer / double  

I did define typecasting for a to be double get my IDE says that there are too many operands + and it somehow complains for not being able to "understand" the +

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

thanks

error message is

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)' 

the compiler can not pick based on signature ? and yes I did define it outside the class because I had one defined inside the class thanks


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


How about putting in a constructor of the form:

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

so that any value which can be cast to a float can be ingested as a Complex. Then, you don't need to make an overloaded operator for all sorts of types. The one you wrote for Complex will suffice.


The reason you're getting the ambiguous overload error is that you have operator+ variants that can add two Complex or a Complex and a double, but you're trying to add a Complex and an int. The compiler can't decide if its better to convert the int to Complex to use the first or to double and use the second.

To avoid this, you need to either define overloaded operator+ for all possible types you might want to add to a Complex (int, float, long, unsigned...) OR not overload operator+ in the first place -- just define a SINGLE operator+ that adds two Complex and let type conversions deal with all the other cases.


Overload operator+ for a double operand:

double Complex::operator+(double rhs)


If you plan to do this, you might want to do the following. First, we define the following (outside the class so that implicit conversion for both the first and second operand is allowed), do NOT define any other operator+, such as 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 the conversion operator (like wheaties pointed out, this can be considered a bad programming practice, which I agree; so if the final conversion to double is not required, omit this):

operator double() const { return real; }

This will automatically support double c = a_complex + 10; and double c = 10 + a_complex;, the number 10 will implicitly converted into a Complex using the implicit constructor and the arithmetic will resolve to operator+(const Complex&, const Complex&);, the result will be converted to double automatically.

P.S. You may also define Complex& operator+=(const Complex& o) { /* .. */ } within the class and use this to implement operator+ above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜