对象具有与成员函数不兼容的类型限定符
Object has type qualifiers that are not compatible with the member function
我的类Game
有一个成员EntityManager entityManager_
。
类EntityManager
有一个私有成员Player player_
和一个返回player_
的公共getter函数Player &EntityManager::getPlayer()
。
类Player
有函数void startMoving()
和sf::Vector2f getPosition() const
。
现在,我可以毫无问题地从我的Game
类中调用entityManager_.getPlayer().startMoving();
,但是当我尝试例如以下代码来获得玩家的位置:
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
我试着从玩家的getPosition函数中删除const
,但没有任何改变。
我知道这可能与const
有关,但我不知道该改变什么!有人能帮帮我吗?
错误信息相当明确:
game.cpp(261): error C2662: 'EntityManager::getPlayer' :
cannot convert 'this' pointer from 'const EntityManager' to
'EntityManager &'
Conversion loses qualifiers
在调用getPlayer
的上下文中,对象/引用是const
。不能在const
对象或const
引用或指向const
的指针上调用非const成员函数。
由于错误指向this
,因此最有可能的原因是该代码位于const
成员函数内部。
相关文章:
- 从数组创建 Mat 时,'cv::Mat' 和 'int' 类型不兼容
- 错误:将"long int (*)[4]"分配给"long int [4][4]"时的类型不兼容
- 操作数类型不兼容("int"和"const char*")
- 不断增加的指针数组 错误:赋值中的类型不兼容
- 操作数类型不兼容("bool (*)()"和"bool")
- C++ 操作数类型不兼容("Movie"和"nullptr")
- 错误:在 GEANY 中将"int"赋值为"char [1]"时类型不兼容
- 使用三元条件运算符时操作数类型不兼容
- C++函数指针类型与 Linux 和 VMS 上的候选指针类型不兼容
- 将“char”分配给“char [100]”的类型不兼容
- C++指向函数作为参数的指针.数据类型不兼容
- 错误:操作数类型不兼容("int"和"const char*")c++
- 与 PVOID 的参数类型不兼容
- 在 malloc 中将"void*"分配给"char"------的类型不兼容
- 错误:将"int"赋值为"int[2]"时的类型不兼容
- 错误:将"char*"分配给"char [20]"时的类型不兼容
- 调用指向派生类成员的指针时,指向与对象类型不兼容的成员类型的指针
- 操作数类型不兼容"BSTR"和"const char*"
- 对float[16]赋值时类型不兼容
- 错误:将'char*'分配给'char [4000]'的类型不兼容