简体   繁体   English

object 具有与成员 function 不兼容的类型限定符。 为什么会出现这个错误?

[英]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.

相关问题 Object具有与成员函数不兼容的类型限定符 - Object has type qualifiers that are not compatible with the member function 该对象具有与成员函数不兼容的类型限定符 - the object has type qualifiers that are not compatible with the member function 复制构造函数错误:对象具有与成员函数不兼容的类型限定符 - copy constructor error: the object has type qualifiers that are not compatible with the member function 该对象具有与成员函数sfml覆盖draw不兼容的类型限定符 - the object has type qualifiers that are not compatible with the member function sfml overriding draw object 的类型限定符与使用向量的成员 function 不兼容 - the object has type qualifiers that are not compatible with the member function using vector IntelliSense:对象具有与成员函数不兼容的类型限定符 - IntelliSense: the object has type qualifiers that are not compatible with the member function 对象具有与成员函数 C++ 不兼容的类型限定符 - The object has type qualifiers that are not compatible with the member function C++ C ++ const”,并且对象具有与成员不兼容的类型限定符 - C++ const "and the object has type qualifiers that are not compatible with the member Object 的类型限定符不兼容,可能是继承/多态错误? - Object has type qualifiers that are not compatible, possible inheritance/polymorphism mistake? 对象具有防止匹配的类型限定符(未找到函数重载) - Object has type qualifiers that prevent match (function overload not found)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM