C++ 成员函数的可访问性似乎随SFML、C+中的范围而变化+;使用Xcode

C++ 成员函数的可访问性似乎随SFML、C+中的范围而变化+;使用Xcode,c++,xcode,sfml,C++,Xcode,Sfml,虽然我可以在创建sf::RectangleShape对象时访问member.setPosition,但我似乎无法在不同的作用域中访问.setPosition成员。帮忙?我是XCODE的新手,但对C++很熟悉,不知道为什么会出错。 class ShapeVisual : public sf::Drawable, public sf::Transformable { public: int fillShape[16]; int shapeWidth; int shapeHei

虽然我可以在创建sf::RectangleShape对象时访问member.setPosition,但我似乎无法在不同的作用域中访问.setPosition成员。帮忙?我是XCODE的新手,但对C++很熟悉,不知道为什么会出错。
class ShapeVisual : public sf::Drawable, public sf::Transformable {
public:
    int fillShape[16];
    int shapeWidth;
    int shapeHeight;

    sf::RectangleShape shapeBlock;
    float shapeBlockWidth;

    ShapeVisual() {
        shapeWidth = 4; shapeHeight = 4;

        Tetrominos::SetShape("T", &fillShape);

        shapeBlockWidth = 10.0;

        shapeBlock = sf::RectangleShape();
        shapeBlock.setPosition(0,0);
        shapeBlock.setOutlineColor(sf::Color::Green);
        shapeBlock.setSize(sf::Vector2f(shapeBlockWidth,shapeBlockWidth));
        shapeBlock.setFillColor(sf::Color(255,100,100));

    }


    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
        states.transform *= getTransform();

        for (int Bx = 0; Bx < this->shapeWidth; Bx++) {
        for (int By = 0; By < this->shapeHeight; By++) {
            shapeBlock.setPosition(Bx*shapeBlockWidth, By*shapeBlockWidth);
            //ERROR HERE: No matching member call for shapeBlock.setPosition.

            if (fillShape[Bx + By*shapeWidth] != 0) {
                target.draw(shapeBlock,states);
            }
        } }

    }
};
以下是sf::RectangleShape类的文档:


编辑:我将shapeBlock更改为指针,现在它似乎可以正常编译和运行。但是我找不到原始代码的问题。

您的
draw
函数是
const
。这意味着您无法修改对象的属性。C++只允许你在属性上调用其他的<代码> const < /Cord>成员函数。在这种情况下,
setPosition
不是
const
成员函数,因此无法编译



显然,当您切换到指针时,您必须做了其他操作才能使其工作。

您能粘贴编译器抛出的确切错误吗?
/Volumes/Minerva/Users/dustinfreeman/Documents/Shapeshifter/Code/Shapeshifter/shapeshifter/shapeshifter/shapes.cpp:147:20: error: no matching member function for call to 'setPosition'
        shapeBlock.setPosition(Bx*shapeBlockWidth, By*shapeBlockWidth);
       ~~~~~~~~~~~^~~~~~~~~~~


/usr/local/include/SFML/Graphics/Transformable.hpp:70:10: note: candidate function not viable: no known conversion from 'const sf::RectangleShape' to 'sf::Transformable' for object argument
void setPosition(float x, float y);
     ^


/usr/local/include/SFML/Graphics/Transformable.hpp:84:10: note: candidate function not viable: requires single argument 'position', but 2 arguments were provided
void setPosition(const Vector2f& position);
     ^