标题:运算符重载时出现问题了
只看楼主
guoqingchun
Rank: 2
等 级:论坛游民
帖 子:30
专家分:68
注 册:2008-6-19
得分:0 
我感觉我这个比较正确,呵呵
#include <iostream>
using namespace std;

class Complex{
private:
    float r,i;
public:
    Complex operator +(Complex &b);
    Complex();
    Complex(float,float);
    void show();
};

Complex Complex::operator +(Complex &b){
    Complex temp;
    temp.r=r+b.r;
    temp.i=i+b.i;
    return temp;
}

Complex::Complex(){r=0;i=0;}
Complex::Complex(float a, float b){
    r=a;i=b;
}
void Complex::show(){
    if(i<0)
        cout<<r<<"-"<<-i<<endl;
    else
        cout<<r<<"+"<<i<<endl;
}

int main(){
    Complex c1(10,11),c2(12,-13),c3;//编译时这里出错了
    c3=c1+c2;
    c3.show();  
    return 0;
}
2008-07-08 15:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-221212-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.085917 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved