samedi 4 novembre 2023

declaring a non static function that takes a variable amount of arguments and then using defining it in the constructor

I have a Button class where each instance will have its callback function with a variable amount of arguments.

*Button.hpp*
class Button{
    private:
        static unsigned short width;
        static unsigned short height;

        sf::RectangleShape* buttonArea;
        std::string text;
        int x,y;
        
        std::function<void()> callback;

    public:
        //Constructors and Destructors
        template <typename ...Args>
        Button(std::string text,std::function<void()> callback ,Args... args);

        
        ~Button();

        bool isClicked(sf::Vector2i pos);

        void onClick();

*Button.cpp*
template <typename ...Args>
Button::Button(std::string text, std::function<void()> callback, Args... args){
    width = 10;
    height = 10;

    this->text = text;

    this->buttonArea = new sf::RectangleShape(sf::Vector2f(width,height));

    this->callback = std::bind(callback, ..args);

};

void onClick(){
    if(this->callback)
        callback();
}

};

The Error appears on onClick where it says 'this' may only be used inside a nonstatic member functionC/C++(258)

Perhaps the use of bind is wrong or the declaration of callback

Aucun commentaire:

Enregistrer un commentaire