# 将运算符重载为友元函数时出了一些莫名其妙的错误

dizengrong 2008-04-13 07:15:08

程序如下：
//头文件
#ifndef COMPLEX_H
#define COMPLEX_H

class Complex
{
private:
double real;
double imag;
public:
Complex() : real(0),imag(0) {}
Complex(double thereal,double theimag) {real=thereal;imag=theimag; }
void setReal(double newreal) {real=newreal; }
void setimag(double newimag) {imag=newimag;}
double getReal() {return real;}
double getImag() {return imag;}
friend Complex operator +(const Complex& z1,const Complex& z2);
friend Complex operator -(const Complex& z1,const Complex& z2);
friend Complex operator *(const Complex& z1,const Complex& z2);
friend Complex operator /(const Complex& z1,const Complex& z2);
friend ostream& operator <<(ostream& out,const Complex& z);
friend istream& operator >>(istream& in,Complex& z);
};
#endif

//类的实现文件
#include "Complex.h"

Complex operator +(const Complex& z1,const Complex& z2)
{
Complex z;
z.real=z1.real+z2.real;
z.imag=z1.imag+z2.imag;
return z;
}
Complex operator -(const Complex& z1,const Complex& z2)
{
Complex z;
z.real=z1.real-z2.real;
z.imag=z1.imag-z2.imag;
return z;
}

Complex operator *(const Complex& z1,const Complex& z2)
{
Complex z;
z.real=z1.real*z2.real-z1.imag*z2.imag;
z.imag=z1.real*z2.imag+z1.imag*z2.real;
return z;
}

Complex operator /(const Complex& z1,const Complex& z2)
{
Complex z;
double m;
m=z2.real*z2.real +z2.imag*z2.imag ;
z.real=(z1.real*z2.real +z1.imag *z2.imag)/m ;
z.imag=(z1.imag*z2.real -z1.real *z2.imag )/m;
return z;
}
ostream& Complex::operator <<(ostream& out,const Complex& z)
{
if(z.imag >0)
cout<<z.real <<"+"<<z.imag <<"i"<<endl;
else if(z.imag <0)
cout<<z.real <<"-"<<z.imag <<"i"<<endl;
else
cout<<z.real <<endl;
return out;
}

istream& Complex::operator >>(istream& in,Complex& z)
{
double n1,n2;
char sign1,sign2;
in>>n1>>sign1>>n2>>sign2;
while(sign2!='i')
{
cout<<"输入错误，请重新输入:"<<endl;
in>>n1>>sign1>>n2>>sign2;
}
z.real =n1;
z.imag =n2;
if(n2<0)
z.imag =-z.imag ;
return in;
}

//测试类的main()函数
#include <iostream>
#include "Complex.h"
#include "Comlex.cpp"
using namespace std;
void main()
{
Complex x,y;
cout<<"输入复数x:";
cin>>x;
cout<<"\n输入复数y:";
cin>>y;
sub=x-y;
div=x*y;
mul=x/y;
cout<<"\nX与Y的差为:"<<sub;
cout<<"\nX与Y的积为:"<<mul;
cout<<"\nX与Y的商为:"<<div;
cout<<"End of testn complex!!!"<<endl;
}

...全文
103 10 打赏 收藏 举报

10 条回复

liujj520 2010-07-30
• 打赏
• 举报

cocoolman 2008-04-14
• 打赏
• 举报

fallenbluefire 2008-04-14
• 打赏
• 举报

//头文件
#ifndef COMPLEX_H
#define COMPLEX_H
include <iostream> // 不加这个ostream，istream哪里来的
..........
..........
friend std::ostream& operator <<(std::ostream& out,const Complex& z);
// 没用using 必须要加上名字空间
friend std::istream& operator >>(std::istream& in,Complex& z);
// 没用using 必须要加上名字空间
};

//类的实现文件
#include "Complex.h"
................
................
ostream& Complex::operator <<(ostream& out,const Complex& z)
istream& Complex::operator >>(istream& in,Complex& z)
// 把Complex::去掉，operator < <是友元函数，不是成员函数
// 记得operator <<的实现里面的cout换成out

//测试类的main()函数
#include <iostream>
#include "Complex.h"
#include "Comlex.cpp" // 去掉这句话

fallenbluefire 2008-04-14
• 打赏
• 举报

//测试类的main()函数
#include <iostream>
#include "Complex.h"
#include "Comlex.cpp" // 这include的了干吗？不要
taodm 2008-04-14
• 打赏
• 举报

dizengrong 2008-04-14
• 打赏
• 举报

dizengrong 2008-04-14
• 打赏
• 举报

dizengrong 2008-04-14
• 打赏
• 举报

谢谢大家的努力与支持！！！
强烈支持ＣＳＤＮ！！！
dizengrong 2008-04-14
• 打赏
• 举报

jieao111 2008-04-13
• 打赏
• 举报

6.3w+

C++ 语言相关问题讨论，技术干货分享，前沿动态等
c++ 技术论坛（原bbs）

2008-04-13 07:15

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下