[英]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.当我使用 getX() 或 getY() function 时,在 lhs 和 rhs object 上出现错误。 The function does not make any changes to the object, it only returns a private float value.
function 不会对 object 进行任何更改,它只返回一个私有浮点值。 I am wondering why this is happening?
我想知道为什么会这样? I am not that great at programming in c++, but I want to learn.
我在 c++ 中编程不是很擅长,但我想学习。
I can take away the const from the lhs and rhs object but I want to know why I get this error.我可以从 lhs 和 rhs object 中删除 const 但我想知道为什么会出现此错误。
Thank you for your assistance.谢谢您的帮助。
This compile error indicated that you try to use a (non-const) member function of a const object.此编译错误表明您尝试使用 const object 的(非常量)成员 function。
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您可以通过将 function 设置为 const 来解决此问题
class A {
void f() const;
}
It means, that no member variables are changed by this function call.这意味着,此 function 调用不会更改任何成员变量。 Thus, it can be applied on a const object.
因此,它可以应用于 const object。
Most likely your getX and getY functions need to be const.您的 getX 和 getY 函数很可能需要为 const。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.