常量不匹配:2 个重载对于“this”没有合法的转换。指针

发布于 2024-11-19 01:56:40 字数 923 浏览 4 评论 0原文

我收到这个奇怪的错误:

错误 C2663: 'sf::Drawable::SetPosition': 2 重载没有合法的转换 “this”指针

我认为它与 const 不匹配有关,但我不知道在哪里,也不知道为什么。 在下面的代码中,我有一个形状和精灵的向量,当尝试访问其中一个向量形状并调用其函数之一时,我收到错误。

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]);
    }
}

我该如何解决这个问题?

I'm getting this weird error:

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

I think it has something to do with const mismatches but I don't know where, or why.
In the following code I have a vector of shapes and sprites, and when trying to access one of the vectors shapes and calling one of its functions I'm getting the error.

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]);
    }
}

How can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们只是彼此的过ke 2024-11-26 01:56:40

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

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

Render is declared with a const after the parameters. This means it does not change its object. Which means, that all of the object's member variables are considered constants within Render, as changing their state means changing the containing object. Assuming Shapes is a member variable, and that SetPosition does change the shape (i.e. not declared as const), you cannot call it within a const member function.

So, remove the const from Render and you'll be fine (you fix your logic, in case it must be const).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文