gpt4 book ai didi

c++ - 常量不匹配 : 2 overloads have no legal conversion for 'this' pointer

转载 作者:IT老高 更新时间:2023-10-28 22:13:02 26 4
gpt4 key购买 nike

我收到了这个奇怪的错误:

error C2663: 'sf::Drawable::SetPosition' : 2 overloads have no legal conversion for 'this' pointer

我认为这与 const 不匹配有关,但我不知道在哪里,或者为什么。在下面的代码中,我有一个形状和 Sprite 的 vector ,当尝试访问其中一个 vector 形状并调用它的一个函数时,我遇到了错误。

std::vector<sf::Shape> Shapes;
std::vector<sf::Sprite> Sprites;

bool AddShape(sf::Shape& S){
Shapes.push_back(S); return true;
};
bool AddSprite(sf::Sprite& S){
Sprites.push_back(S); return true;
};

private:

virtual void Render(sf::RenderTarget& target) const {
for(unsigned short I; I<Shapes.size(); I++){
Shapes[I].SetPosition(
Shapes[I].GetPosition().x + GetPosition().x,
Shapes[I].GetPosition().y + GetPosition().y);
target.Draw(Shapes[I]);
}
for(unsigned short I; I<Sprites.size(); I++){
target.Draw(Sprites[I]);
}
}

我该如何解决这个问题?

最佳答案

Render 在参数后用 const 声明。这意味着它不会改变它的对象。这意味着,所有对象的成员变量都被视为 Render 中的常量,因为更改它们的状态意味着更改包含对象。假设 Shapes 是一个成员变量,并且 SetPosition 确实改变了形状(即未声明为 const),你不能在 const 成员函数。

所以,从 Render 中删除 const 就可以了(你修复你的逻辑,以防它必须是 const)。

关于c++ - 常量不匹配 : 2 overloads have no legal conversion for 'this' pointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6639264/

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