简体   繁体   中英

Accesibility of member functions seems to vary with scope in SFML, C++ with Xcode

While I can access the member .setPosition while making an sf::RectangleShape object, I seem to not be able to access the .setPosition member in a different scope. Help? I am new to Xcode, but familiar to C++ and not sure why this would cause an error.

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);
            }
        } }

    }
};

The exact text of the error is

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

Here's the documentation for the sf::RectangleShape class: http://www.sfml-dev.org/documentation/2.0/classsf_1_1RectangleShape.php

EDIT: I changed shapeBlock to be a pointer, and now it seems to compile and run fine. But I can't find the issue with the original code.

Your draw function is const . Which means you cannot modify the object's attributes. C++ only allows you to call other const member function on your attributes. In this case, setPosition is not a const member function so it fails to compile.


When you switched to a pointer you must have done something else to make it work, obviously.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM