Const不匹配:2个重载没有'this'指针的合法转换

Gri*_*fin 17 c++ const vector this-pointer

我收到这个奇怪的错误:

错误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]);
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

era*_*ran 24

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

所以,删除constfrom Render并且你会没事(你修复你的逻辑,以防它必须是const).