12

我的班级Game有一个成员EntityManager entityManager_

该类EntityManager有一个私有成员和返回Player player_的公共 getter 函数。Player &EntityManager::getPlayer()player_

该类Player具有例如函数void startMoving()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

输出:

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

我尝试const从播放器的 getPosition 函数中删除,但没有任何改变。

我知道这可能与该有关,const但我不知道要更改什么!有人可以帮我吗?

4

1 回答 1

20

错误消息非常明确:

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

在您调用getPlayer对象/引用的上下文中是const. 您不能在const对象上或通过const引用或指针调用非常量成员函数const

因为错误指的是this,最可能的原因是这段代码是在一个成员函数里面const

于 2014-07-10T13:00:16.173 回答