![]() |
#2
ljt2010-09-23 22:30
|

#include"iostream.h"
#include"math.h"
class complex
{
double real,image;
public
complex(double areal=0.0,double aimage=0.0):real(areal),image(aimage){}
~complex(){};
operator double();
};
double complex::operator double()//转换成员函数
{
double n;
n=real*real+image*image;
return sqrt(n);
}
void main()
{
complex ob(1.1,2.2);
double x=12;
cout<<"x+ob.double()="<<x+ob<<endl;
}
--------------------Configuration: 1 - Win32 Debug--------------------#include"math.h"
class complex
{
double real,image;
public
complex(double areal=0.0,double aimage=0.0):real(areal),image(aimage){}
~complex(){};
operator double();
};
double complex::operator double()//转换成员函数
{
double n;
n=real*real+image*image;
return sqrt(n);
}
void main()
{
complex ob(1.1,2.2);
double x=12;
cout<<"x+ob.double()="<<x+ob<<endl;
}
Compiling...
1.cpp
D:\VC98\转换成员函数\1.cpp(7) : error C2320: expected ':' to follow access specifier 'resolved identifier'
D:\VC98\转换成员函数\1.cpp(12) : error C2549: user-defined conversion cannot specify a return type
D:\VC98\转换成员函数\1.cpp(21) : error C2264: 'operator`double'' : error in function definition or declaration; function not called
D:\VC98\转换成员函数\1.cpp(21) : fatal error C1903: unable to recover from previous error(s); stopping compilation
执行 cl.exe 时出错.
1.exe - 1 error(s), 0 warning(s)
如题,请大家帮忙解答