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

研究半天,没弄出来,求大神指点,谢谢!

jiekevv520 发布于 2015-05-17 21:47, 662 次点击
//一个复数类Complex,重载运算符“+”使之能够完成附属的加法运算。
#include "stdafx.h"
#include <iostream>
using namespace std;
class Complex
{public:
//Complex() {real=0;imag=0;}
Complex(double r=0,double i=0):real(r),imag(i) {}
Complex operator+ (Complex &c2);
friend Complex operator+ (Complex &c,int &i);
friend Complex operator+ (int &i,Complex &c2);
void display();
private:
    double real;
    double imag;
};
Complex Complex::operator+ (Complex &c2)
{return Complex(real+c2.real,imag+c2.imag);
}
Complex operator+(Complex &c,int &i)
{return Complex(i+c.real,c.imag);
}
Complex operator+ (int &i,Complex &c2)
{return (i+c2.real,c2.imag);
};
void Complex::display()
{cout<<'('<<real<<','<<imag<<'i)'<<endl;
}
int main ()
{Complex r1(1,2),r2(3,4),r3;
r3 = r1 + r2;
r3 = r2 + r1;
r3 = r1 + 4;  
r3 = 4 + r1;
cout<<"r1=";r1.display();
cout<<"r2=";r2.display();
cout<<"r1+r2=";r3.display();
return 0;
}
7 回复
#2
诸葛欧阳2015-05-17 21:55
编译信息是什么
#3
yangfrancis2015-05-18 13:53
Complex Complex::operator+ (Complex &c2)
{return Complex(real+c2.real,imag+c2.imag);
}
会不会是传值与传址的差异问题?你参数用了取地址符的,函数体写成return Complex(real+c2->real,imag+c2->imag);连报错位置和报错信息都没提供出来,只能瞎猜。
如果的确是这个问题,你后面同样的错误也要相应地修改才能运行。
#4
wp2319572015-05-18 14:07
在网上找的能运行的一个复数类  可以参考一下:

程序代码:

# include <iostream>  
using namespace std;  
class my_complex {  
private:  
    int real;  
    int imag;  
public:  
    my_complex();  
    my_complex(int real, int imag);  
    ~my_complex();  
    my_complex(const my_complex& rhs);  
    my_complex& operator=(const my_complex& rhs);  
    my_complex& operator+(const my_complex& rhs);  
    bool operator==(const my_complex& rhs);  
    friend ostream &operator<<(ostream& output, const my_complex &rhs);  
};  
my_complex::my_complex() :  
    real(0), imag(0) {  
}  
my_complex::my_complex(int real, int imag) :  
    real(real), imag(imag) {  
}  
my_complex::my_complex(const my_complex& rhs) {  
    real = rhs.real;  
    imag = rhs.imag;  
}  
my_complex& my_complex::operator +(const my_complex& rhs) {  
    real = rhs.real + real;  
    imag = rhs.imag + imag;  
    return *this;  
}  
//需要处理自我赋值(还好,因为这里没有动态内存的分配)  
my_complex& my_complex::operator =(const my_complex& lhs) {  
    real = lhs.real;  
    imag = lhs.imag;  
    return *this;  
}  
bool my_complex::operator ==(const my_complex& rhs) {  
    return real == rhs.real && imag == rhs.imag;  
}  
my_complex::~my_complex() {  
  
}  
ostream& operator<<(ostream& output, const my_complex &rhs) {  
    output << rhs.real;  
    if (rhs.imag > 0) {  
        std::cout << "+";  
    }  
    std::cout << rhs.imag << "i";  
    return output;  
}  
int main() {  
    my_complex c1(1, 5);  
    my_complex c2(c1); //等价于 my_complex c2 = c1;  
    my_complex c3(2, 3);  
    bool flag(c1 == c2);  
    c1 = c1 + c2;  
    cout << c1 << endl;  
    cout << c2 << endl;  
    cout << c3 << endl;  
    cout << flag << endl;  
}  
#5
诸葛欧阳2015-05-18 14:28
回复 4楼 yangfrancis
这是引用
#6
yangfrancis2015-05-18 15:16
发现了,看了一下它的声明
#7
jiekevv5202015-05-22 12:38
谢谢各位!
1