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

INTERNAL COMPILER ERROR 怎么改

糊涂无罪 发布于 2012-06-06 10:26, 400 次点击

#include <iostream>
using namespace std;

class complex{
public:
    complex(double r=0.0,double i=0.0){
    real=r;
    image=i;}
   
friend complex operator+( const complex &c1,const complex &c2);
private:
    double real;
    double image;
};
complex operator+( const complex &c1,const complex &c2);{
    real=c1.real+c2.real;
    image=c1.image+c2.image;
    return complex(real,image);}
int main(){
     complex c3,c4,c5;
c3(1.0,2.0);
c4(2.0,3.0);
cout<<"c3="<<c3<<endl;
cout<<"c4="<<c4<<endl;
c5=c3+c4;
cout<<"c3+c4="<<c5<<endl;
c3(1.0);
c4(2.0,3.0);
cout<<"c3="<<c3<<endl;
cout<<"c4="<<c4<<endl;
c5=c3+c4;
cout<<"c3+c4="<<c5<<endl;
c3(1.0,2.0);
c4(2.0);
cout<<"c3="<<c3<<endl;
cout<<"c4="<<c4<<endl;
c5=c3+c4;
cout<<"c3+c4="<<c5<<endl;
return 0;}
1 回复
#2
hellovfp2012-06-06 12:24
#include <iostream>
using namespace std;

class complex{
public:
    complex(double r=0.0,double i=0.0){
        real=r;
        image=i;}
    complex operator += (const complex &rhs)
    {
        real += rhs.real;
        image += rhs.image;
        return *this;
    }
    friend ostream& operator << (ostream &os, const complex &rhs)
    {
        cout << rhs.real << ":" << rhs.image << endl;
        return os;
    }
private:
    double real;
    double image;
};

complex operator+( const complex &c1,const complex &c2){
    complex temp(c1);
    return temp += c2;
}

int main()
{
   
    complex c5;
    complex c3(1.0,2.0);
    complex c4(2.0,3.0);

    c5 = c3 + c4;
    cout<<"c3="<<c3<<endl;
    cout<<"c4="<<c4<<endl;
    cout << "c5=" << c5 << endl;

    return 0;
}
1