c++ - 成员函数的可访问性似乎随 SFML、C++ 和 Xcode 中的范围而变化

标签 c++ xcode sfml

虽然我可以在创建 sf::RectangleShape 对象时访问成员 .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);
            }
        } }

    }
};

错误的确切文本是

/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);
     ^

这是 sf::RectangleShape 类的文档:http://www.sfml-dev.org/documentation/2.0/classsf_1_1RectangleShape.php

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

最佳答案

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


显然,当您切换到指针时,您必须执行其他操作才能使其正常工作。

关于c++ - 成员函数的可访问性似乎随 SFML、C++ 和 Xcode 中的范围而变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19128701/

相关文章:

C++:*(乘)运算符的结合性不是从左到右

swift - 从后台转换到事件状态后,如何恢复 mp4 视频? Xcode 10.1, swift 4.2

c++ - 如何确保粒子始终在中心对齐

c++ - 具有 SFML 对象的 Box2D 在碰撞后重叠并相互重叠

c++ - std::fstream::tellg() 输出文件光标指针不正确?

c++ - 使用 Boost 反序列化 Armadillo colvec

c++ - 为什么我的 istream_iterator 不起作用?

ios - Xcode 9.1 - 对成员过滤器的引用不明确

ios - 引用 Storyboard中的后退按钮

c++ - 如何在 while 循环中使用时钟