rl529014
好儿郎-志在四方
采纳率100%
2015-11-29 06:15 阅读 2.0k

c++用成员函数和友元函数实现复数的加减操作,产生了二义性,麻烦各位大哥大姐解答一下,感激不尽

1

我想在一个程序中用友元函数和类的成员函数实现复数的加减操作,
类中这样定义:
图片说明

main函数中用这几句调用的话存在二义性:
图片说明

c3=c1+c2;这一句就不知道到底该调用成员函数还是调用友元函数,但是重载的话只有参数类型和参数个数不同,就应该没问题的,这里该怎么解决呢?
麻烦各位大哥大姐了;下面是程序的代码,

#include

class Complex
{
public:
Complex(double r=0.0,double i=0.0):real(r),imag(i){}
Complex operator+ (const Complex &c2)const;
Complex operator- (const Complex &c2)const;

    friend Complex operator+(const Complex &c1,const Complex &c2);
    friend Complex operator-(const Complex &c1,const Complex &c2);


    friend ostream &operator<< (ostream &out,const Complex &c);
    void display()const;
private:
    double real;
    double imag;            

} ;

Complex Complex::operator+ (const Complex &c2)const
{
return Complex(real+c2.real,imag+c2.imag);
}
Complex Complex::operator- (const Complex &c2)const
{
return Complex(real-c2.real,imag-c2.imag);
}

void Complex::display()const
{
cout<<"("<<real<<","<<imag<<")"<<endl;
}

Complex operator+(const Complex &c1,const Complex &c2)
{
return Complex(c1.real+c2.real,c1.imag+c2.imag);
}
Complex operator-(const Complex &c1,const Complex &c2)
{
return Complex(c1.real-c2.real,c1.imag-c2.imag);
}
ostream &operator<< (ostream &out,const Complex &c)
{
out<<"("<<c.real<<","<<c.imag<<")"<<endl;
return out;
}

int main()
{
Complex c1(5,4),c2(2,10),c3;
c1.display();
c2.display();
c3.display();

c3=c1+c2;
c3.display();
cout<<c3;

c3=c3-c1;
c3.display();
cout<<c3;

return 0;

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

4条回答 默认 最新

  • 已采纳
    u011665148 求弱批 2015-11-29 08:38

    你在函数里写个输出就能看出优先级了,具体我也没试过,无法给出正确答案

    点赞 1 评论 复制链接分享
  • qq_27183003 ysuwood 2015-11-29 08:45
     c3=c1+c2;//对象+对象
     用成员函数 Complex operator+ (const Complex &c2)const;
    
     c3=4+c2; //数+对象
     用友元函数
     friend Complex operator+ (double r, const Complex &c2);
    
    
    点赞 评论 复制链接分享
  • rl529014 好儿郎-志在四方 2015-11-29 11:00

    图片说明
    谢谢两位哈,太感谢了,我用这种方式消除了二义性;
    真正写代码的时候肯定不会这么用的,我只是想知道该怎么消除二义性。谢谢两位哈

    点赞 评论 复制链接分享
  • John_ToString John_ToString 2015-12-02 07:34

    #include
    using namespace std;

    class Complex
    {
    public:
    Complex(double r = 0.0, double i = 0.0) :real(r), imag(i){}
    //Complex operator+ (const Complex &c2)const;
    //Complex operator- (const Complex &c2)const;
    friend Complex operator+(const Complex &c1, const Complex &c2);
    friend Complex operator-(const Complex &c1, const Complex &c2);

    friend ostream &operator<< (ostream &out, const Complex &c);
    void display()const;
    

    private:
    double real;
    double imag;
    };
    //Complex Complex::operator+ (const Complex &c2)const
    //{
    // return Complex(real + c2.real, imag + c2.imag);
    //}
    //Complex Complex::operator- (const Complex &c2)const
    //{
    // return Complex(real - c2.real, imag - c2.imag);
    //}
    void Complex::display()const
    {
    cout << "(" << real << "," << imag << ")" << endl;
    }
    Complex operator+(const Complex &c1, const Complex &c2)
    {
    return Complex(c1.real + c2.real, c1.imag + c2.imag);
    }
    Complex operator-(const Complex &c1, const Complex &c2)
    {
    return Complex(c1.real - c2.real, c1.imag - c2.imag);
    }
    ostream &operator<< (ostream &out, const Complex &c)
    {
    out << "(" << c.real << "," << c.imag << ")" << endl;
    return out;
    }

    int main()
    {
    Complex c1(5, 4), c2(2, 10), c3;
    c1.display();
    c2.display();
    c3.display();
    c3 = c1 + c2;
    c3.display();
    cout << c3;

    c3 = c3 - c1;
    c3.display();
    cout << c3;
    
    return 0;
    

    }

    你这样写必然产生二义性...弄清楚什么事表像,什么事本质...

    点赞 评论 复制链接分享

相关推荐