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

谁说胖子不能爱 提交于 2019-12-10 12:49:11

问题


My class Game has a member EntityManager entityManager_.

The class EntityManager has a private member Player player_ and the public getter function Player &EntityManager::getPlayer() which returns player_.

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

Now, I can without problems call entityManager_.getPlayer().startMoving(); from within my Game class, but when I try for example the following code to get the player's position:

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.

I know it probably has something to do with the const but I can't figure out what to change! Could someone please help me?


回答1:


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. You cannot call a non-const member function on a const object or through a const reference or pointer to const.

Because the error refers to this, the most likely reason is that this code is inside a member function that is const.



来源:https://stackoverflow.com/questions/24677032/object-has-type-qualifiers-that-are-not-compatible-with-the-member-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!