Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Drawable and a cv-qualifier(what is this even?)  (Read 2913 times)

0 Members and 1 Guest are viewing this topic.

Ilman

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
sf::Drawable and a cv-qualifier(what is this even?)
« on: March 06, 2014, 08:46:55 pm »
I've been trying to get into object oriented C++ for the last couple of weeks, so today I tried to start a project (that being a simple calculator  :P, everybody's gotta start somewhere). So, I made a simple Button class.
Basically, what I have in Button.h is:
class Button : public sf::Drawable
{
    private:
          sf::RectangleShape rect;
          virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
          .....
}
 
While in Button.cpp I have
void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    target.draw(rect, states);
}
 

The errors that pop up are:
D:\SFML projects\Calculator\Button.cpp|74|error: non-member function 'void draw(sf::RenderTarget&, sf::RenderStates)' cannot have cv-qualifier|
D:\SFML projects\Calculator\Button.cpp||In function 'void draw(sf::RenderTarget&, sf::RenderStates)':|
D:\SFML projects\Calculator\Button.cpp|76|error: 'rect' was not declared in this scope|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 0 seconds) ===|
   
Built with Code::Blocks(MinGW) and SFML 2.1 (32-bit SJLJ version).

I'm fairly certain I'm doing something wrong, so I'd like some help in understanding how sf::Drawable should be integrated into my class.

All help would be really appreciated.   :)

Full code(didn't find a spoiler tag, so I guess I'll have to leave it like this):

Button.h
#ifndef ADD_H
#define ADD_H
#include "SFML/Graphics.hpp"

class Button : public sf::Drawable
{
    bool markedflag;
    sf::RectangleShape rect;
    sf::Color unmarked, marked, clicked;
    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
public:
    Button();
    Button(int x, int y, int a, int b);
    Button(sf::Vector2f a, sf::Vector2f b);
    Button(sf::IntRect a);

    void checkMouse(sf::Vector2f mous);

    void setRect ();
    void setRect (int x, int y, int a, int b);
    void setRect (sf::Vector2f a, sf::Vector2f b);
    void setRect (sf::IntRect a);

    void setColors (sf::Color col1, sf::Color col2, sf::Color col3);

    void updateColor ();
};

#endif

Button.cpp

#include "Button.h"

Button::Button()
{
    rect.setPosition(0, 0);
    rect.setSize(sf::Vector2f(0, 0));
}

Button::Button(int x, int y, int a, int b)
{
    rect.setPosition(x, y);
    rect.setSize(sf::Vector2f(a, b));
}

Button::Button(sf::Vector2f a, sf::Vector2f b)
{
    rect.setPosition(a);
    rect.setSize(b);
}

Button::Button(sf::IntRect a)
{
    rect.setPosition(a.left, a.top);
    rect.setSize(sf::Vector2f(a.width, a.height));
}

void Button::setRect()
{
    rect.setPosition(0, 0);
    rect.setSize(sf::Vector2f(0, 0));
}

void Button::setRect(int x, int y, int a, int b)
{
    rect.setPosition(x, y);
    rect.setSize(sf::Vector2f(a, b));
}

void Button::setRect(sf::Vector2f a, sf::Vector2f b)
{
    rect.setPosition(a);
    rect.setSize(b);
}

void Button::setRect(sf::IntRect a)
{
    rect.setPosition(a.left, a.top);
    rect.setSize(sf::Vector2f(a.width, a.height));
}

void Button::setColors(sf::Color col1, sf::Color col2, sf::Color col3)
{
    unmarked=col1;
    marked=col2;
    clicked=col3;
}

void Button::updateColor()
{
    if (markedflag==0) rect.setFillColor(unmarked);
    else if (markedflag==1) rect.setFillColor(marked);
    else rect.setFillColor(clicked);
}

void Button::checkMouse(sf::Vector2f mous)
{
    sf::Vector2f pos=rect.getPosition(), siz=rect.getSize();
    if (pos.x<=mous.x&&pos.y<=mous.y&&siz.x>=mous.x&&siz.y>=mous.y) {if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) markedflag=2;
                                                                     else markedflag=1;}
    else markedflag=0;

}

void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    target.draw(rect, states);
}
 

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: sf::Drawable and a cv-qualifier(what is this even?)
« Reply #1 on: March 06, 2014, 10:22:32 pm »
You forgot to scope draw in the .cpp. :)

Should be:
void Button::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    target.draw(rect, states);
}
 

Ilman

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: sf::Drawable and a cv-qualifier(what is this even?)
« Reply #2 on: March 06, 2014, 10:38:06 pm »
And, as I was assuming, my mistake was incredibly dumb.   ;D
Thanks, everything works now.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Drawable and a cv-qualifier(what is this even?)
« Reply #3 on: March 07, 2014, 10:38:33 am »
By the way, a CV qualifier is either const or volatile. In this case the compiler error message was misleading, but knowing the term helps in other situations.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: