[求助]为什么这样不能实现隐式类型转换?
定义复数类Complex头文件中:
#include <iostream>
using namespace std;
class Complex
{
friend Complex operator + (Complex&, Complex&);
public:
Complex();
Complex(double);//将双精度型转换成Complex型
Complex(double, double);
...
private:
double real;
double imag;
};
Complex::Complex(double r)
{
real = r;
imag = 0;
}
……
在main文件中
有
……
Complex c1(3,-4);
c2 = 4.0 + c2;
……
编译时g++说
$ g++ -c main.cpp
main.cpp: In function `int main()':
main.cpp:26: error: no match for 'operator+' in '4.0e+0 + c2'
complex.h:17: note: candidates are: Complex operator+(Complex&, Complex&)
这是为什么?不是系统会隐式调用Complex(double)吗?
还有friend Complex operator + (Complex&, Complex&);
我重载了+
friend Complex operator + (Complex& c1, Complex& c2)
{
return Complex(c1.real + c2.real, c1.imag + c2.imag);
}
[[it] 本帖最后由 haitaotao 于 2008-10-10 15:54 编辑 [/it]]