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();
from within my Game
class, but when I try for example the following code to get the player's position:
现在,我可以毫无问题地调用entityManager_.getPlayer()。startMoving();从我的Game类中,但是当我尝试以下代码来获取玩家的位置时:
sf::Vector2f playerPosition = entityManager_.getPlayer().getPosition();
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! Could someone please help me?
我知道它可能与const有关但我无法弄清楚要改变什么!有人可以帮帮我吗?
1 个解决方案
#1
15
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
.
在您调用getPlayer的上下文中,对象/引用是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
.
因为错误引用了这个,最可能的原因是这个代码在const的成员函数中。
#1
15
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
.
在您调用getPlayer的上下文中,对象/引用是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
.
因为错误引用了这个,最可能的原因是这个代码在const的成员函数中。