c++编程的一道练习题,就是给复数类重载运算符,要求是用友元函数,我编的编译老是有一个error,求改

编码如下:#include<iostream>usingnamespacestd;classComplex{doublereal,image;public:Complex... 编码如下:
#include<iostream>using namespace std;class Complex{ double real,image;public: Complex(double r=0.0,double i=0.0){real=r;image=i;} Complex(Complex &com){ real=com.real;image=com.image; } void print(){ cout<<"Real="<<real<<'\t'<<"Image="<<image<<endl; } friend Complex operator-(Complex,Complex); friend Complex operator-=(Complex,Complex); friend Complex operator*=(Complex,Complex); friend Complex operator/=(Complex,Complex); Complex operator++(); Complex operator++(int);};Complex operator-(Complex c1,Complex c2){ Complex temp(c1.real-c2.real,c1.image-c2.image); return temp;}Complex operator -= (Complex c1,Complex c2){ c1.real=c1.real-c2.real; c1.image=c1.image-c2.image; return c1;}Complex Complex::operator /= (Complex c1,Complex c2){ Complex temp; double d=c2.real*c2.real+c2.image*c2.image; temp.real =(c1.real*c2.real+c1.image*c2.image)/d; temp.image =(c1.image*c2.real-c1.real*c2.image)/d; c1.real=temp.real; c2.image=temp.image; return temp;}Complex Complex::operator *= (Complex c1,Complex c2){ Complex temp; temp.real =c1.real*c2.real-c1.image*c2.image; temp.image=c1.image*c2.real+c1.real*c2.image; c1.real=temp.real; c2.image=temp.image; return temp;}Complex Complex::operator ++ (){ return Complex(++real,image);}Complex Complex::operator ++ (int){ return Complex(real++,image);}int main(){ Complex c1(1.0,1.0),c2(2.0,2.0),c3(4.0,4.0),c; c1.print(); c=c2-c3;c.print (); c-=c1;c.print(); c1++;c1.print (); return 0;}
展开
 我来答
记忆是C
2013-11-25
知道答主
回答量:1
采纳率:0%
帮助的人:1438
展开全部
  1. 把第一行的<iostream>改为<iostream.h>

  2. 把第二行内容去掉

  3. 把以下两行中的complex都去掉一个

    Complex Complex::operator /= (Complex c1,Complex c2){

    Complex Complex::operator *= (Complex c1,Complex c2){

  4. 最终程序为:

    #include<iostream.h>
    class Complex{
     double real,image;
    public:
     Complex(double r=0.0,double i=0.0){real=r;image=i;}
     Complex(Complex &com){
      real=com.real;image=com.image;
     }
     void print(){
      cout<<"Real="<<real<<'\t'<<"Image="<<image<<endl;
     }
     friend Complex operator-(Complex,Complex);
     friend Complex operator-=(Complex,Complex);
     friend Complex operator*=(Complex,Complex);
     friend Complex operator/=(Complex,Complex);
     Complex operator++();
     Complex operator++(int);
    };
    Complex operator-(Complex c1,Complex c2){
     Complex temp(c1.real-c2.real,c1.image-c2.image);
     return temp;
    }
    Complex operator -= (Complex c1,Complex c2){
     c1.real=c1.real-c2.real;
     c1.image=c1.image-c2.image;
     return c1;
    }
    Complex operator /= (Complex c1,Complex c2){
     Complex temp;
     double d=c2.real*c2.real+c2.image*c2.image;
     temp.real =(c1.real*c2.real+c1.image*c2.image)/d;
     temp.image =(c1.image*c2.real-c1.real*c2.image)/d;
     c1.real=temp.real;
     c2.image=temp.image;
     return temp;
    }
    Complex operator *= (Complex c1,Complex c2){
     Complex temp;
     temp.real =c1.real*c2.real-c1.image*c2.image;
     temp.image=c1.image*c2.real+c1.real*c2.image;
     c1.real=temp.real;
     c2.image=temp.image;
     return temp;
    }
    Complex Complex::operator ++ (){
     return Complex(++real,image);
    }
    Complex Complex::operator ++ (int){
     return Complex(real++,image);
    }
    int main(){
     Complex c1(1.0,1.0),c2(2.0,2.0),c3(4.0,4.0),c;
     c1.print();
     c=c2-c3;c.print ();
     c-=c1;c.print();
     c1++;c1.print ();
     return 0;
    }

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
yyjie999
2013-11-20 · TA获得超过199个赞
知道小有建树答主
回答量:197
采纳率:100%
帮助的人:181万
展开全部
1)Complex::operator /= ()不需要加限定符号==》::operator /()
2)

Complex Complex::operator ++ (){
return Complex(++real,image);
}

实现有问题:
Complex Complex::operator ++ (){
real++;
return *this;
}
最好改成如下的形式:
Complex &Complex::operator ++ (){
real++;
return *this;
}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式