The best match for an operator
When I am compiling the following program
#include "stdafx.h"
#include <iostream>
#include <complex>
class Complex_
{
public:
double real, image;
Complex_(): real( 0 ), image( 0 ) {}
Complex_( double theReal, double theImage = 0.0 ):
real( theReal ), image( theImage ) {}
Complex_ & operator+=( const Complex_ & rhs )
{
real += rhs.real;
image += rhs.image;
return *this;
}
const Complex_ operator+( double d )
{
return Complex_( real + d );
}
operator double() const
{
return real;
}
};
const Complex_ operator+(const Complex_& lhs, const Complex_& rhs)
{
return Complex_( lhs )+= rhs;
}
int _tmain(int argc, _TCHAR* argv[])
{
Complex_ c1( 10, 15 ), c2( 12, 14 );
Complex_ c3;
c3 = c1.operator+( c2 );
std::cout << "c1.real = " << c1.real << ", c1.image = " << c1.image <<
std::endl;
std::cout << "c2.real = " << c2.real << ", c2.image = " << c2.image <<
std::endl;
std::cout << "c3.real = " << c3.real << ", c3.image = " << c3.image <<
std::endl;
c3 = c1 + c2;
std::cout << "c1.real = " << c1.real << ", c1.image = " << c1.image <<
std::endl;
std::cout << "c2.real = " << c2.real << ", c2.image = " << c2.image <<
std::endl;
std::cout << "c3.real = " << c3.real << ", c3.image = " << c3.image <<
std::endl;
return 0;
}
I have gotten the errors
complex.cpp(51) : error C2666: 'Complex_::operator +' : 3 overloads have
similar conversions
complex.cpp(23): could be 'const Complex_ Complex_::operator +(double)'
complex.cpp(35): or 'const Complex_ operator +(const Complex_ &,const
Complex_ &)'
or 'built-in C++ operator+(double, double)'
while trying to match the argument list '(Complex_, Complex_)'
note: qualification adjustment (const/volatile) may be causing the ambiguity
Is not the operator 'const Complex_ operator +(const Complex_ &,const
Complex_ &)' best match for the statement c3 = c1 + c2;, is it?
Vladimir Grigoriev