简体   繁体   English

Object具有与成员函数不兼容的类型限定符

[英]Object has type qualifiers that are not compatible with the member function

My class Game has a member EntityManager entityManager_ . 我的类Game有一个成员EntityManager entityManager_

The class EntityManager has a private member Player player_ and the public getter function Player &EntityManager::getPlayer() which returns player_ . EntityManager类有一个私有成员Player player_和公共getter函数Player &EntityManager::getPlayer() ,它返回player_

The class Player has for example the functions void startMoving() and sf::Vector2f getPosition() const . Player具有例如void startMoving()sf::Vector2f getPosition() const

Now, I can without problems call entityManager_.getPlayer().startMoving(); 现在,我可以毫无问题地调用entityManager_.getPlayer().startMoving(); from within my Game class, but when I try for example the following code to get the player's position: 从我的Game类中,但是当我尝试以下代码来获取玩家的位置时:

sf::Vector2f playerPosition = entityManager_.getPlayer().getPosition();

I get the following error: 我收到以下错误:

IntelliSense: 智能感知:

EntityManager Game::entityManager_

Error: the object has type qualifiers that are not compatible with the member function

object type is: const EntityManager

Output: 输出:

game.cpp(261): error C2662: 'EntityManager::getPlayer' : cannot convert 'this' pointer from 'const EntityManager' to 'EntityManager &'
          Conversion loses qualifiers

I tried removing the const from the player's getPosition function but nothing changed. 我尝试从播放器的getPosition函数中删除const但没有改变。

I know it probably has something to do with the const but I can't figure out what to change! 我知道它可能与const但我无法弄清楚要改变什么! Could someone please help me? 有人可以帮帮我吗?

The error message is quite explicit: 错误消息非常明确:

game.cpp(261): error C2662: 'EntityManager::getPlayer' : 
               cannot convert 'this' pointer from 'const EntityManager' to 
                                                  'EntityManager &'
          Conversion loses qualifiers

In the context where you are calling getPlayer the object/reference is const . 在您调用getPlayer的上下文中,对象/引用是const You cannot call a non-const member function on a const object or through a const reference or pointer to const . 您不能在const对象上调用非const成员函数,也不能通过const引用或指向const指针调用非const成员函数。

Because the error refers to this , the most likely reason is that this code is inside a member function that is const . 因为错误引用了this ,最可能的原因是这个代码在const的成员函数中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 该对象具有与成员函数不兼容的类型限定符 - 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 复制构造函数错误:对象具有与成员函数不兼容的类型限定符 - copy constructor error: 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 具有与成员 function 不兼容的类型限定符。 为什么会出现这个错误? - the object has type qualifiers that are not compatible with the member function. Why is this error showing up? 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