在 c++ 中重载运算符 +,-,*,/ 时出错

Error while overloading operator +,-,*,/ in c++

本文关键字:出错 c++ 重载 运算符      更新时间:2023-10-16

我在尝试编译以下代码时出错。我是初学者。请帮我弄清楚。我正在尝试重载运算符 _,+,/,*。它在编译时显示错误。我应该使用"朋友"来解决这个问题吗?

#include<stdio.h>
class complex{
private:
    double real; //real part of complex
    double imag; // imaginary part of complex
public:
    complex(double r=0., double i=0.):real(r),imag(i)
    {
    } // constructor with initialization
    complex(const complex&c):real(c.real),imag(c.imag)
    {
    } // copy constructor with initialization
    ~complex()
    {
    } // destructor
    double re() const
    {
        return real;
    } // read real part
    double im() const
    {
        return imag;
    } // read imaginary part
    const complex& operator=(const complex&c)
    {
        real=c.real;
        imag=c.imag;
        return *this;
    } //assignment operator
    const complex& operator+=(const complex&c)
    {
        real += c.real;
        imag += c.imag;
        return *this;
    } // addition of current complex
    const complex& operator-=(const complex&c)
    {
        real -= c.real;
        imag -= c.imag;
        return *this;
    } // subtract from current complex
    const complex& operator*=(const complex&c)
    {
        double keepreal = real;
        real = real*c.real-imag*c.imag;
        imag = keepreal*c.imag+imag*c.real;
        return *this;
    } // multiply current complex with a complex
    const complex& operator/=(double d)
    {
        real /= d;
        imag /= d;
        return *this;
    } // divide current complex with real
    void print(const complex&c)
    {
        printf("(%f,%f)n",c.re(),c.im() );
    } // printing complex number
    friend complex operator !(const complex& c)
    {
        return complex(c.re(),-c.im());
    }
    friend double abs2(const complex& c)
    {
        return c.re()*c.re()+c.im()*c.im();
    } // absolute value of complex
    const complex& operator/=(const complex&c)
    {
        return *this *= (!c)/=abs2(c);
    } // divide the current complex by a complex
    const complex operator-(const complex& c)
    {
        return complex(-c.re(),-c.im());
    } // negative of complex number
    const complex operator-(const complex& c,const complex& d)
    {
        return complex(c.re()-d.re(),c.im()-d.im());
    } // difference between complex numbers
    const complex operator+(const complex& c,const complex& d)
    {
        return complex(c.re()+d.re(),c.im()+d.im());
    } // addition of complex numbers
    const complex operator*(const complex& c,const complex& d)
    {
        return complex(c)*=d;
    } // multiplication of complex numbers
    const complex operator/(const complex& c,const complex& d)
    {
        return complex(c)/=d;
    } // division of complex numbers
};
int main(){
    complex c(1.,0.),d(3.,4.);
    print(c-d);
    print(c/d);
    return 0;
}

我得到的输出是:

complex_nums.cpp:76:59: error: ‘const complex complex::operator-(const complex&, const complex&)’ must take either zero or one argument
  const complex operator-(const complex& c,const complex& d)
                                                           ^
complex_nums.cpp:80:59: error: ‘const complex complex::operator+(const complex&, const complex&)’ must take either zero or one argument
  const complex operator+(const complex& c,const complex& d)
                                                           ^
complex_nums.cpp:84:59: error: ‘const complex complex::operator*(const complex&, const complex&)’ must take either zero or one argument
  const complex operator*(const complex& c,const complex& d)
                                                           ^
complex_nums.cpp:88:59: error: ‘const complex complex::operator/(const complex&, const complex&)’ must take exactly one argument
  const complex operator/(const complex& c,const complex& d)
                                                           ^
complex_nums.cpp: In function ‘int main()’:
complex_nums.cpp:96:11: error: ‘print’ was not declared in this scope
  print(c-d);
           ^
complex_nums.cpp:97:9: error: no match for ‘operator/’ (operand types are ‘complex’ and ‘complex’)
  print(c/d);

你所有的运算符(+,-,*,/(只需要一个参数或不需要参数,除非它们是友元函数。将所有operator函数标记为friend,代码应该可以工作。否则,从每个运算符中删除 1 个参数,而不是使用当前实例 ( this (。删除参数+运算符的示例:

const complex operator+(const complex& c)
{
    return complex(c.re()+re(),c.im()+im());
} 

friend算术运算符参考:http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators-using-friend-functions/

下一个错误

你用你的print()函数做什么?它应该在全局范围内,因为它将complex作为参数。像这样将print()从类移到全局范围内。如果您仍希望为对象本身保留print(),请这样做,但您的类应如下所示:

class complex
{
    void print(const complex&c)
    {
        printf("(%f,%f)n",re(),im() );
    } // printing complex number
};
void print(const complex&c)
{
    printf("(%f,%f)n",c.re(),c.im() );
} // printing complex number

二进制运算符要么是具有 2 个参数的自由函数(首选(,要么是具有一个参数的成员函数(不太好(。

您已使用 2 个参数定义了成员函数。

通过添加friend将它们转换为免费函数是一种方式。

另一种方法是使它们成为自由函数,在类外部定义,但根据成员函数编写(无论如何你已经这样做了(:

struct complex
{
 /* ... */ 
};
// class definition ends
//free function
inline complex operator/(const complex& c,const complex& d)
{
    return complex(c)/=d;
} // division of complex numbers

注意,此功能可以改进:

inline complex operator/(complex c,const complex& d)
{
    c /= d;
    return c;
}

此外,一元运算符应该返回 complex& ,而不是 const complex& 。你返回的东西是可变的,因为你刚刚改变了它。

对于运算符的错误 must take either zero or one argument ,这是因为您正在将非成员函数实现为成员函数。这些应该是正常工作的自由函数,或者应该只接受一个参数并使用this作为另一个参数。

有关说明,请参阅此问题:

运算符重载:成员函数与非成员函数?

对于打印错误,我猜您正在尝试调用打印成员函数。为此,您需要执行以下操作:

complex(c-d).print();