Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 对象具有与成员函数不兼容的类型限定符_C++_Types_Constants_Compatibility_Qualifiers - Fatal编程技术网

C++ 对象具有与成员函数不兼容的类型限定符

C++ 对象具有与成员函数不兼容的类型限定符,c++,types,constants,compatibility,qualifiers,C++,Types,Constants,Compatibility,Qualifiers,我的类Game有一个成员EntityManager EntityManager\uu 类EntityManager有一个私有成员Player-Player和公共getter函数Player&EntityManager::getPlayer(),该函数返回Player 例如,类Player具有函数void startMoving()和sf::Vector2f getPosition()const 现在,我可以毫无问题地调用entityManager_uz.getPlayer().startMovi

我的类
Game
有一个成员
EntityManager EntityManager\uu

EntityManager
有一个私有成员
Player-Player
和公共getter函数
Player&EntityManager::getPlayer()
,该函数返回
Player

例如,类
Player
具有函数
void startMoving()
sf::Vector2f getPosition()const

现在,我可以毫无问题地调用
entityManager_uz.getPlayer().startMoving()在我的
游戏
类中,但当我尝试以下代码以获取玩家的位置时:

sf::Vector2f playerPosition=entityManager_zy.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
我试图从播放器的getPosition函数中删除
常量,但没有任何改变


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

错误信息非常明确:

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

成员函数中,我怀疑您没有getPlayer的const版本,但您没有显示任何代码。将
const
添加到
getPlayer
会删除错误,但是你能告诉我我必须返回什么而不是
player\uuuu
?因为现在
getPlayer
告诉我:错误:将类型“Player&”的引用中的限定符丢弃到类型“constplayer”@A.D.的初始值设定项中:您是否在声明为
const
的成员函数中?@DavidRodríguez dribeas啊,是的。该成员函数实际上是
const
。我把它取下来了,现在它开始工作了!谢谢你需要
Player&EntityManager::getPlayer(){return\u Player;}
const Player&EntityManager::getPlayer()const{return\u Player;}