gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 17:21:31 25 4
gpt4 key购买 nike

我的类 Game 有一个成员 EntityManager entityManager_

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

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 的指针调用非常量成员函数。

因为错误指的是this,最可能的原因是这段代码在const的成员函数中。

关于c++ - 对象具有与成员函数不兼容的类型限定符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24677032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com