views:

137

answers:

5

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

+4  A: 
double operator+(const Complex &c, int x)
{
    //....
}
Brian R. Bondy
does it belong to complex class or should it be defined outside of the class ? because I tried this and it says too many operands for operator + :(
stdnoit
@stdnoit: As I gave an example it belongs outside of the class. If you want it inside of the class you'd omit the first arg.
Brian R. Bondy
stdnoit
i have edited my previous post with the error message thanks
stdnoit
+1  A: 

Overload operator+ for a double operand:

double Complex::operator+(double rhs)
Maulrus
stdnoit
Maulrus
+3  A: 

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.

wheaties
A: 

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.

Chris Henry
That conversion `operator double() const { return real; }` is considered bad practice. As a matter of fact, it's the first entry in the operators section of Scott Myer's More Effective C++.
wheaties
It is. However, it answers the need of automatic conversion to double (I don't personally encourage such automatic conversion, but the OP requested that). If the OP does not require the result to be assigned back to a double but to a complex, omit operator double() const. :)
Chris Henry
i really dont know why this is an error and why the compiler does not pick which method to call based on function error C2666: 'Complex::operator +' : 3 overloads have similar conversionsi have operator+ overloaded and defined inside the class. it is one of the requirement of the class i have the operator double() which does the typecasting ( even though wheaties says it is bad. the professor wanted it )and i have one that is defined outside the class. and another one built in C++ operator(double, int) apparently the program can not pick which one to be called on :(
stdnoit
Chris Henry
+1  A: 

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.

Chris Dodd