| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 899 人关注过本帖
标题:复数相加问题解决不了
只看楼主 加入收藏
cwl168
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2012-12-14
结帖率:8.33%
收藏
 问题点数:0 回复次数:10 
复数相加问题解决不了
#include<iostream.h>

class Complex
{
private:
    float real,imag;
public:
    Complex(){ real=0;imag=0;}
    Complex(float a,float b):real(a),imag(b){}
    Complex operator+(Complex &c);
    Complex operator +(int &i);
    friend ostream& operator<<(ostream &,Complex &);
    friend Complex operator +(Complex &,int &);
    void display();

};
Complex Complex::operator+(Complex &c)
{
    return Complex(real+c.real,imag+c.imag);
}

void Complex::display()
{

    cout<<real<<"+"<<imag<<"i"<<endl;
}
ostream &operator<<(ostream &ouput,Complex &c)
{
     ouput<<c.real<<"+"<<c.imag<<"i"<<endl;
     return ouput;
}
Complex Complex::operator +(int &i)
{     
    return Complex(real+i,imag);
}
Complex operator+(Complex &c,int &i)
{
     return Complex(c.real+i,c.imag);
}
int main()
{
    Complex c1(1,2),c2(2,3),c3,c4;
    //c3=c1+c2;
   
    c4=c2+4;
    cout<<c4;
    return 0;
}

--------------------Configuration: 10_3 - Win32 Debug--------------------
Compiling...
10_3.cpp
F:\Study Garden\上机内容\c++\10_3\10_3.cpp(45) : error C2679: binary '+' : no operator defined which takes a right-hand operand of type 'const int' (or there is no acceptable conversion)
执行 cl.exe 时出错.

10_3.obj - 1 error(s), 0 warning(s)
搜索更多相关主题的帖子: include display void private Complex 
2013-01-11 16:54
云剑转身
Rank: 1
等 级:新手上路
帖 子:7
专家分:4
注 册:2012-11-25
收藏
得分:0 
加的类型不一样,把主函数改成这样的试试
int main()
{
    Complex c1(1,2),c2(2,3),c3,c4,c5(4,0);
    //c3=c1+c2;
   
    c4=c2+c5;
    cout<<c4;
    return 0;
}
2013-01-11 17:18
玩出来的代码
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:河南新乡
等 级:贵宾
威 望:11
帖 子:742
专家分:2989
注 册:2009-10-12
收藏
得分:0 
binary '+' : no operator defined which takes a right-hand operand of type 'const int' 看懂这句 就明白了,写的很清楚了

离恨恰如春草,更行更远还生。
2013-01-11 17:22
cwds
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:23
专家分:134
注 册:2012-6-17
收藏
得分:0 
错误原因在于:c4=c2+4;的右操作数4是个整型的常量字面值。而你重载的 + 操作符要求传入的是个引用。你可以选择:
第1种:
    Complex operator +(int &i); ==> Complex operator +(int i);    //去掉引用
第2种:
    int i = 4;
    c4 = c2 + i;    //用变量代替,变量存在存储空间,所以才可以取引用

但这样改完后(假设按第2种方式修改),又会出现新的问题。VS2008编译器会报:“operator +”不明确。原因在于,你定义了 Complex operator +(int &i); 和 friend Complex operator +(Complex &,int &); 两个方法,上面的语句 c4 = c2 + i; 在尝试匹配方法时不知应该匹配这两个中的哪个。你可以选择去掉这两个方法中的某一个。
2013-01-11 19:01
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
我觉得楼主最好还是换书换老师
2013-01-12 08:26
globc
Rank: 2
等 级:论坛游民
帖 子:15
专家分:12
注 册:2013-1-20
收藏
得分:0 
形参前加const
2013-01-20 10:39
xixifans
Rank: 1
等 级:新手上路
帖 子:118
专家分:2
注 册:2012-11-15
收藏
得分:0 
重载的时候左右也有差别的~~~~~~~~~~~

看那没有形象的发型和憔悴的脸就知道这是一只和C++恋爱的宅女。yes,I am!
2013-01-22 14:57
caoboxx
Rank: 1
来 自:广西柳州
等 级:新手上路
帖 子:10
专家分:9
注 册:2013-1-22
收藏
得分:0 
Complex operator+(Complex &c);
     friend Complex operator +(Complex &,int &);
这两个函数有冲突的,这不是实现相同的功能了。注释掉其中以个
2013-01-22 16:14
caoboxx
Rank: 1
来 自:广西柳州
等 级:新手上路
帖 子:10
专家分:9
注 册:2013-1-22
收藏
得分:0 
我错了。的确是那个4的问题,可以定义一个变量 int i=4,因为你函数的声明和定义时是变量的引用,而在调用时用的是常量,这里出现了问题。
修改后编译还是会有问题的,出现的问题就是上面我提出的那个了,可以将上两个函数注释掉一个就OK了。
#include<iostream.h>

class Complex
{
private:
    float real,imag;
public:
    Complex(){ real=0;imag=0;}
    Complex(float a,float b):real(a),imag(b){}
    Complex operator+(Complex &c);
    Complex operator +( int &i);
    friend ostream& operator<<(ostream &,Complex &);
 //  friend Complex operator +(Complex &,int &);
    void display();

};
Complex Complex::operator+(Complex &c)
{
    return Complex(real+c.real,imag+c.imag);
}

void Complex::display()
{

    cout<<real<<"+"<<imag<<"i"<<endl;
}
ostream &operator<<(ostream &ouput,Complex &c)
{
     ouput<<c.real<<"+"<<c.imag<<"i"<<endl;
     return ouput;
}

Complex Complex::operator +(int &i)
{     
    return Complex(real+i,imag);
}
/*
Complex operator+(Complex &c,int &i)
{
     return Complex(c.real+i,c.imag);
}
*/
int main()
{
    Complex c1(1,2),c2(2,3),c3,c4;
    //c3=c1+c2;
   int i=4;
    c4=c2+i;
    cout<<c4;
    return 0;
}
这是修改后的代码你看看~~
2013-01-22 16:58
caoboxx
Rank: 1
来 自:广西柳州
等 级:新手上路
帖 子:10
专家分:9
注 册:2013-1-22
收藏
得分:0 
回复 4楼 cwds
有见地
2013-01-22 17:00
快速回复:复数相加问题解决不了
数据加载中...
 
   



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

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