常量不匹配:2 个重载对于“this”没有合法的转换。指针
我收到这个奇怪的错误:
错误 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Render
在参数后面使用const
进行声明。这意味着它不会改变其对象。这意味着,对象的所有成员变量都被视为 Render 中的常量,因为更改它们的状态意味着更改包含的对象。假设Shapes
是一个成员变量,并且SetPosition
确实改变了形状(即未声明为const
),您不能在代码>const
成员函数。因此,从
Render
中删除const
就可以了(你可以修复你的逻辑,以防它必须是 const)。Render
is declared with aconst
after the parameters. This means it does not change its object. Which means, that all of the object's member variables are considered constants withinRender
, as changing their state means changing the containing object. AssumingShapes
is a member variable, and thatSetPosition
does change the shape (i.e. not declared asconst
), you cannot call it within aconst
member function.So, remove the
const
fromRender
and you'll be fine (you fix your logic, in case it must be const).