注册 登录
编程论坛 C++教室

程序错哪了?

nbaqqqq 发布于 2010-09-23 17:59, 1486 次点击
程序代码:
#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--------------------
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)
如题,请大家帮忙解答
3 回复
#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();
};
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;
}
#3
m21wo2010-09-23 22:35
#include <iostream>
#include <cmath>
using namespace std;
class Complex
{
    double real,image;
public:
        Complex(double areal=0.0,double aimage=0.0):real(areal),image(aimage){}
        ~Complex(){}
   
    operator 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;
}
#4
nbaqqqq2010-09-24 11:58
书上错的啊,谢谢你们
1