我的课Game
有一个会员EntityManager entityManager_
.
班上EntityManager
有一个私人成员Player player_
和公共 getter 函数Player &EntityManager::getPlayer()
返回player_
.
班上Player
例如具有以下功能void startMoving()
and sf::Vector2f getPosition() const
.
现在,我可以毫无问题地打电话entityManager_.getPlayer().startMoving();
从我的内心Game
类,但是当我尝试使用以下代码来获取玩家的位置时:
sf::Vector2f playerPosition = entityManager_.getPlayer().getPosition();
我收到以下错误:
智能感知:
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
我尝试删除const
来自玩家的 getPosition 函数,但没有任何改变。
我知道这可能与const
但我不知道要改变什么!有人可以帮我吗?
错误信息非常明确:
game.cpp(261): error C2662: 'EntityManager::getPlayer' :
cannot convert 'this' pointer from 'const EntityManager' to
'EntityManager &'
Conversion loses qualifiers
在您调用的上下文中getPlayer
对象/引用是const
。您不能在 a 上调用非常量成员函数const
对象或通过const
引用或指针const
.
因为错误指的是this
,最可能的原因是这段代码位于一个成员函数内,该函数是const
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)