【问题标题】:Error while overloading operators重载运算符时出错
【发布时间】:2016-09-02 12:12:40
【问题描述】:

当我尝试超载运算符“!”时,会出现以下错误。


complex_nums.cpp: In function ‘complex operator!(const complex&)’:
complex_nums.cpp:50:23: error: passing ‘const complex’ as ‘this’ argument discards qualifiers [-fpermissive]
   return complex(c.re(),-c.im());
                       ^
complex_nums.cpp:14:9: note:   in call to ‘double complex::re()’
  double re(){
         ^
complex_nums.cpp:50:31: error: passing ‘const complex’ as ‘this’ argument discards qualifiers [-fpermissive]
   return complex(c.re(),-c.im());
                               ^
complex_nums.cpp:17:9: note:   in call to ‘double complex::im()’
  double im(){
     ^

代码是:


#include<iostream>

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(){
        return real;
    }; // read real part
    double im(){
        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(){
        std::cout<<"Real: "<<re()<<"   Imaginary: "<<im()<<"\n";
    };
    friend complex operator !(const complex& c){
        return complex(c.re(),-c.im());
    };
};

int main(){
    complex C(1.,1.);
    complex P(3.,2.);
    C.print();
    P.print();
    P+=C;
    P.print();
    P=!C;
    P.print();
    return 0;
}

【问题讨论】:

  • 提示在错误信息中
  • 尽可能将参数声明为const。您可以使用非常量调用函数,但不能反过来

标签: c++ class operator-overloading overloading operator-keyword


【解决方案1】:

这是线索……

错误:将“const complex”作为“this”参数传递会丢弃限定符

问题在于 im()re() 不是 const 方法。

【讨论】:

    【解决方案2】:

    使用限定符 const 声明这些函数

    double re() const {
        return real;
    }; // read real part
    double im() const {
        return imag;
    }; // read imaginary part
    

    因为至少在运算符中

    friend complex operator !(const complex& c){
        return complex(c.re(),-c.im());
    };
    

    它们被称为常量对象。

    【讨论】:

      【解决方案3】:

      这是规则,当你创建 const 函数时,传递给这个 const 函数的函数也必须是 const.. re() 和 im() 必须是 const

      【讨论】:

        猜你喜欢
        • 2012-03-25
        • 1970-01-01
        • 2014-03-18
        • 2016-09-25
        • 2014-04-24
        • 2021-10-29
        • 2016-03-17
        • 1970-01-01
        • 2012-08-21
        相关资源
        最近更新 更多