简体   繁体   中英

the object has type qualifiers that are not compatible with the member function. Why is this error showing up?

PVector operator + (const PVector& lhs, const PVector& rhs){
    return PVector(lhs.getX() + rhs.getX(), lhs.getY()+ rhs.getY());
}

I get an error on the lhs and rhs object when I use the getX() or getY() function. The function does not make any changes to the object, it only returns a private float value. I am wondering why this is happening? I am not that great at programming in c++, but I want to learn.

I can take away the const from the lhs and rhs object but I want to know why I get this error.

Thank you for your assistance.

This compile error indicated that you try to use a (non-const) member function of a const object.

class A {
    void f(); 
}

const A a;
A.f(); // <- this will result in a cv qualifier compile error.

You can fix this by setting the function to const

class A {
    void f() const; 
}

It means, that no member variables are changed by this function call. Thus, it can be applied on a const object.

Most likely your getX and getY functions need to be 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