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