C++中系统类型与自定义类型隐式转换与重载函数参数是否引用有什么关系?

简单描述,当重载 + 号参数为非引用时可以实现隐式转换
但若加上引用符号则不行,求解?

错误提示:
error: no match for 'operator+' (operand types are 'Complex' and 'double')|

#include <iostream>
using namespace std;

class Complex
{
public:

    Complex(){real=0,imag=0;}
    Complex(double r, double i){real=r, imag=i;}
    Complex operator - (Complex &c);
    Complex operator * (Complex &c);
    Complex operator / (Complex &c);
    Complex(double r){real=r,imag=0;}
    friend Complex operator+(Complex &c1, Complex &c2);
    //operator double(){return real;}
    void display();
private:
    double real;
    double imag;
};

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

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

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

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

Complex operator + (Complex &c1,Complex &c2)
{
    return (Complex(c1.real+c2.real, c1.imag+c2.imag));
}

int main(void)
{
    Complex c1(2,4), c2(3,6), c3;
    double i = 7.0;
    c1.display(), c2.display();
    c3 = c2 + i;
    c3.display();
    c3 = c2 - c1;
    c3.display();
    c3 = c2 * c1;
    c3.display();
    c3 = c2 / c1;
    c3.display();

    return 0;
}
c++

1个回答

const &能正常调用,运算符重载参数一般都使用const &
至于为啥Complex &不行就不知道了,编译器似乎不认这个函数

Complex operator + (const Complex &c1, const Complex &c2);
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问