Argument and pointer error

Feb 18, 2018 at 8:20pm
I have a class:
1
2
3
4
5
6
7
8
9
10
//Button.hpp
class Button : public sf::Drawable
{
  public:
    sf::Text OptionalText;
    sf::Vector2f ButtonPos; //sf::Vector2f contains a coordinate in floats (x, y)
  private:
    virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const;

};


And I have a cpp file:

1
2
3
4
5
6
7
void Button::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
    sf::Vector2f temporary =  OptionalText.getPosition(); //Ok...
    this->OptionalText.setPosition(ButtonPos  + temporary); //Error
    target.draw(OptionalText, states);
    this->OptionalText.setPosition(temporary); //Error
}


And sf::Text::setPosition is defined as:

 
  void 	setPosition (const Vector2f &position);


When compiling I get:

1
2
In member function 'virtual void Button::draw(sf::RenderTarget&, sf::RenderStates) const':
error: passing 'const sf::Text' as 'this' argument of 'void sf::Transformable::setPosition(const Vector2f&)' discards qualifiers [-fpermissive]

And a similar error for the second time I call Text.setPosition()


Edit: Forgot to say that Button gets its draw function from sf::Drawable
Last edited on Feb 18, 2018 at 9:08pm
Feb 18, 2018 at 8:26pm
You will likely have to remove the const qualifiers to make it work. The: 'discards qualifiers' part suggests that this is the culprit.
Last edited on Feb 18, 2018 at 8:27pm
Feb 18, 2018 at 8:34pm
I unfortunately can't remove the const in the setPosition function as though it's from a library.
Also, when I try to remove it from the setPosition function, I get an error when I try to use it saying that I can't declare the object to be of abstract type 'Button'
Last edited on Feb 18, 2018 at 8:34pm
Feb 18, 2018 at 8:48pm
This is understood, yes, you can't. :)

The point is that you seem to try to use non-const qualified objects in your function. So, it is worth a try to remove const from this function, and also virtual, which should take care of the other error. (Likely, anyway.)

virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const;

If this doesn't do, maybe:

const_cast<sf::Text*>(this)->OptionalText.setPosition(ButtonPos + temporary);
const_cast<sf::Text*>(this)->OptionalText.setPosition(temporary);

does the trick.
Feb 18, 2018 at 8:54pm
I don't think removing const is a good idea. A draw function shouldn't modify the object that is being drawn. The best option would be if you could come up with a way that avoids calling setPosition from inside the Button::draw function.
Feb 18, 2018 at 9:09pm
Actually, even if I do remove the virtual and the const, I still get the abstract type error, (look at my edit) the Button class inherits from sf::Drawable
Feb 18, 2018 at 9:24pm
May I ask why you need to have a separate ButtonPos variable? Can't you just set the position of OptionalText directly?

If you want the Button position to be different from the Text position I would recommend making the variables private (in good OOP style) and adding a Button::setPosition function that takes care of setting the positions correctly. That way the Text position could always be kept correct and would not have to be modified inside the draw function.
Last edited on Feb 18, 2018 at 9:26pm
Feb 18, 2018 at 9:39pm
Aha! Good idea! Yes, in reality the button class looks like [link=https://pastebin.com/Mb5xGMbR]so[/link] and it's meant to have an optional text inside, and the position for the text would be relative to the Button's position. Thank you! I will edit the setPosition function for the button and add a setTextPosition function. Thanks!
Topic archived. No new replies allowed.