| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 583 人关注过本帖
标题:运算符的重载
只看楼主 加入收藏
呜呜1
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2013-9-10
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:7 
运算符的重载
#include<iostream>
using  namespace std;
class  Complex
{public:
Complex(double  real=0,double  imag=0);
friend  Complex  operator  *(const  Complex  &c1,const Complex &c2);
friend  Complex  operator  /(const  Complex  &c1,const Complex &c2);

friend  bool  operator  ==(const  Complex  &c1 ,const Complex &c2);
friend  bool operator  !=(const  Complex  &c1,const Complex &c2);
friend  ostream & operator <<(ostream &,Complex &);
friend  istream & operator >>(istream &,Complex &);
 void  compare();

    double  real,imag;
};
//以上是函数声明
Complex::Complex(double r,double i):real(r),imag(i){}
Complex  operator *(const  Complex &c1,const Complex &c2 )
{return Complex(c1.real*c2.real-c1.imag*c2.imag,c1.imag*c2.real+c1.real*c2.imag);}
Complex  operator /(const  Complex &c1,const Complex &c2 )
{return   Complex((c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag),(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag));}
ostream & operator <<(ostream & output ,Complex &c)
{ output<<"("<<c.real<<"+"<<c.imag<<"i)"<<endl;
return  output;
}
istream & operator >>(istream & input ,Complex &c)
{input>>c.real>>c.imag;
return  input;
}

bool  operator ==(const  Complex &c1,const Complex &c2 )
{if((c1.real==c2.real)&&(c1.imag==c2.imag))
return  true;
else  return  false;
}
bool  operator !=(const  Complex &c1,const Complex &c2 )
{if((c1.real!=c2.real)||(c1.imag!=c2.imag))
return  true;
else  return  false;
}
void Complex::compare(const  Complex &c1,const Complex &c2 )
{if(operator== (c1,c2)==1)
    cout<<"c1==c2";
   else  cout<<"c1!=c2";
}
//以上是函数定义

int  main()
{Complex c1,c2,c3;
cin>>c1;
cin>>c2;
cout<<"c1="<<c1;
cout<<"c2="<<c2;
c3=c1*c2;
cout<<"c3="<<c3;
c3=c1/c2;
cout<<"c3="<<c3;
c1=c2;
cout<<"c1="<<c1;
cout<<"c2="<<c2;
compare(c1,c2);
return 0;
}
//我想实现的功能是修改该类,使之能用重载的>>和<<运算符输入和输出复数;
   2) 重载乘法和除法运算符,使之能执行两个复数的代数乘法和代数除法运算;
   3) 重载运算符==和!=,使之能比较两个复数;
   4) 重载运算符=,使之能进行两个复数的赋值运算。
程序运行后报错,说我compare函数没定义,晕啊
搜索更多相关主题的帖子: Complex include public double friend 
2013-11-17 18:12
i80286
Rank: 6Rank: 6
等 级:侠之大者
威 望:5
帖 子:99
专家分:428
注 册:2013-9-30
收藏
得分:4 
既然你定义了Complex c1,c2,c3且Complex类里你又定义了double  real,imag;那么cin>>c1和cin>>c2明显就有错误,不可以这样对变量进行赋值,好好去看下书
2013-11-17 19:25
呜呜1
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2013-9-10
收藏
得分:0 
回复 2楼 i80286
我学的是运算符的重载,重载了 cin    cout ,可以cin>>c1,cin>>c2这样使用的
2013-11-17 19:43
i80286
Rank: 6Rank: 6
等 级:侠之大者
威 望:5
帖 子:99
专家分:428
注 册:2013-9-30
收藏
得分:0 
我知道你想干什么,但自定义的类变量怎么可以这样赋值呢?
假设你写的没错,那问一下:cin>>c1,你想让机器读入什么数据(实数部分还是虚数部分)?

[ 本帖最后由 i80286 于 2013-11-17 19:53 编辑 ]
2013-11-17 19:49
呜呜1
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2013-9-10
收藏
得分:0 
回复 4楼 i80286

cin>>c1;就是让用户自己输入复数的实部 , 虚部,你看看下面这个程序的主函数,就是这样输入数据的,我建了一个头文件两个源文件,可以运行,就是“比较两个复数大小的功能未实现”




#include<iostream>
using  namespace std;
class  Complex
{public:
Complex(double  real=0,double  imag=0);
friend  Complex  operator  *(const  Complex  &c1,const Complex &c2);
friend  Complex  operator  /(const  Complex  &c1,const Complex &c2);

friend  bool  operator  ==(const  Complex  &c1 ,const Complex &c2);
friend  bool operator  !=(const  Complex  &c1,const Complex &c2);
friend  ostream & operator <<(ostream &,Complex &);
friend  istream & operator >>(istream &,Complex &);
 
    double  real,imag;
};





/////////////////////////////////////


#include"fushu.h"
Complex::Complex(double r,double i):real(r),imag(i){}
Complex  operator *(const  Complex &c1,const Complex &c2 )
{return Complex(c1.real*c2.real-c1.imag*c2.imag,c1.imag*c2.real+c1.real*c2.imag);}
Complex  operator /(const  Complex &c1,const Complex &c2 )
{return   Complex((c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag),(c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag));}
ostream & operator <<(ostream & output ,Complex &c)
{ output<<"("<<c.real<<"+"<<c.imag<<"i)"<<endl;
return  output;
}
istream & operator >>(istream & input ,Complex &c)
{input>>c.real>>c.imag;
return  input;
}

bool  operator ==(const  Complex &c1,const Complex &c2 )
{if((c1.real==c2.real)&&(c1.imag==c2.imag))
return  true;
else
 return  false;
}

bool  operator !=(const  Complex &c1,const Complex &c2 )
{if((c1.real!=c2.real)||(c1.imag!=c2.imag))
return  true;
else  
return  false;
}



//////////////////////////////////////////////////


#include"fushu.h"
int  main()
{Complex c1,c2,c3;
cin>>c1;
cin>>c2;
cout<<"c1="<<c1;
cout<<"c2="<<c2;
c3=c1*c2;
cout<<"c3="<<c3;
c3=c1/c2;
cout<<"c3="<<c3;
c1=c2;
cout<<"c1="<<c1;
cout<<"c2="<<c2;
cout<<(c1!=c2)<<endl;
cout<<(c1==c2)<<endl;
system("pause");
return 0;
}




2013-11-17 21:05
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:4 
帮你排版了一下
程序代码:
#include <iostream>

class Complex
{
public:
    Complex( double real=0, double imag=0 );
    friend Complex operator *( const Complex& c1, const Complex& c2 );
    friend Complex operator /( const Complex& c1, const Complex& c2 );

    friend bool operator ==( const Complex& c1, const Complex& c2 );
    friend bool operator !=( const Complex& c1, const Complex& c2 );
    friend std::ostream& operator <<( std::ostream&, const Complex& );
    friend std::istream& operator >>( std::istream&, Complex& );
    friend void compare( const Complex& c1, const Complex& c2 );

    double real,imag;
};

using namespace std;

Complex::Complex( double r, double i ) : real(r), imag(i)
{
}
Complex operator *( const Complex& c1, const Complex& c2 )
{
    return Complex( c1.real*c2.real-c1.imag*c2.imag, c1.imag*c2.real+c1.real*c2.imag );
}
Complex operator /(const Complex& c1,const Complex& c2 )
{
    return Complex( (c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag), (c1.imag*c2.real-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag) );
}
ostream& operator <<( ostream& output, const Complex& c)
{
    return output<<"("<<c.real<<"+"<<c.imag<<"i)";
}
istream& operator >>( istream& input, Complex& c )
{
    input>>c.real>>c.imag;
    return input;
}

bool operator ==( const Complex& c1, const Complex& c2 )
{
    return c1.real==c2.real && c1.imag==c2.imag;
}
bool operator !=( const Complex& c1, const Complex& c2 )
{
    return c1.real!=c2.real || c1.imag!=c2.imag;
}
void compare( const Complex& c1, const Complex& c2 )
{
    if(operator== (c1,c2)==1)
        cout<<"c1==c2";
    else
        cout<<"c1!=c2";
}

int main()
{
    Complex c1,c2,c3;
    cin >> c1;
    cin >> c2;
    cout << "c1=" << c1 << endl;
    cout << "c2=" << c2 << endl;
    c3 = c1*c2;
    cout << "c1*c2" << c3 << endl;
    c3 = c1/c2;
    cout << "c1/c2=" << c3 << endl;
    c1=c2;
    cout<<"c1="<<c1 << endl;
    cout<<"c2="<<c2 << endl;
    compare(c1,c2);

    return 0;
}

2013-11-18 08:43
blueskiner
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:227
专家分:707
注 册:2008-9-22
收藏
得分:0 
乱糟糟的代码风格,看到人想吐
2013-11-18 09:22
左手拉一只猫
Rank: 4
来 自:杭州
等 级:业余侠客
帖 子:70
专家分:250
注 册:2013-10-27
收藏
得分:12 
(1)你的代码木有缩进实在是很伤(2)你的程序出错是因为你在类中声明的友元函数compare()和你在类外定义的友元函数的是不匹配的(也就是这两个东西不对应,所以在编译时候出错误了,有图有真相)
void  compare();

void Complex::compare(const  Complex &c1,const Complex &c2 )
这两货参数都不对应的,而且你要在下面程序中以compare(a,b)的形式调用函数,应该在类内声明compare时候把声明为friend类型的额,不然就默认为累的成员函数了。(正确答案楼上已经给出,我就不贴了)
还有就是
if(operator== (c1,c2)==1)
既然都已经重载运算符了,这句话也可以简化成
if(c1==c2)
效果是一样的,还容易看懂。
说完了,坐等楼主给分。

我能帮你的只能到这里了。。。
2013-11-18 12:55
快速回复:运算符的重载
数据加载中...
 
   



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

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