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

dizengrong 2008-04-13 07:15:08
我构建了一个复数类Complex,并且重载了+,-,*,/与<<,>>为友元函数
可编译时的错误让我感觉莫名其妙啊!请大哥大姐们一定要帮帮我,我实在是对
此无能为力了,麻烦大家了.
    程序如下:
//头文件
#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;
Complex add,sub,div,mul;
add=x+y;
sub=x-y;
div=x*y;
mul=x/y;
cout<<"\nX与Y的和为:"<<add;
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
  • 打赏
  • 举报
回复
在Complex.h文件和Complex.cpp文件中加:#include <iostream>
在Complex.cpp文件中再加:using namespace std;

错误提示是啥?
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" // 去掉这句话

作业贴可能,lz好好学习要
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
  • 打赏
  • 举报
回复
我的编译器为visual studio 2005(即7.0的),应该算很新的吧
dizengrong 2008-04-14
  • 打赏
  • 举报
回复
程序OK了
     谢谢大家的努力与支持!!!
  强烈支持CSDN!!! 
dizengrong 2008-04-14
  • 打赏
  • 举报
回复
看到大家给我回复,我太高兴了,首先得谢谢大家拉,特别是fallenbluefire,为我挑出了这么多的错误.
我先去改一下程序,回头在告诉大家
jieao111 2008-04-13
  • 打赏
  • 举报
回复
你的什么编译器,v6对友元支持不好
相关推荐
发帖
C++ 语言

6.3w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
帖子事件
创建了帖子
2008-04-13 07:15
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下