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

复数相加问题解决不了

cwl168 发布于 2013-01-11 16:54, 913 次点击
#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)
10 回复
#2
云剑转身2013-01-11 17:18
加的类型不一样,把主函数改成这样的试试
int main()
{
    Complex c1(1,2),c2(2,3),c3,c4,c5(4,0);
    //c3=c1+c2;
   
    c4=c2+c5;
    cout<<c4;
    return 0;
}
#3
玩出来的代码2013-01-11 17:22
binary '+' : no operator defined which takes a right-hand operand of type 'const int' 看懂这句 就明白了,写的很清楚了
#4
cwds2013-01-11 19:01
错误原因在于: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; 在尝试匹配方法时不知应该匹配这两个中的哪个。你可以选择去掉这两个方法中的某一个。
#5
rjsp2013-01-12 08:26
我觉得楼主最好还是换书换老师
#6
globc2013-01-20 10:39
形参前加const
#7
xixifans2013-01-22 14:57
重载的时候左右也有差别的~~~~~~~~~~~
#8
caoboxx2013-01-22 16:14
Complex operator+(Complex &c);
     friend Complex operator +(Complex &,int &);
这两个函数有冲突的,这不是实现相同的功能了。注释掉其中以个
#9
caoboxx2013-01-22 16:58
我错了。的确是那个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;
}
这是修改后的代码你看看~~
#10
caoboxx2013-01-22 17:00
回复 4楼 cwds
有见地
#11
waterstar2013-01-23 17:06
建议:
当传入的是内置类型的参数时,直接使用值传递,引用传递最大的优势是速度,内置类型的速度不是问题。而且一般传递的时候使用const 引用
1