简体   繁体   中英

Error while overloading operators

When I try to over load an operator "!", it gives error as following.


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(){
     ^

The code is:


#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;
}

This is the clue...

error: passing 'const complex' as 'this' argument discards qualifiers

The problem is that im() and re() are not const methods.

Declare these functions with the qualifier const

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

because at least in the operator

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

they are called for a constant object.

这是规则,当您创建const function时,传递给该const函数的函数也必须为const。re()和im()必须为const

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM