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?
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).