lundi 21 septembre 2020

How to pass any member function as argument to global function's std::function parameter? [duplicate]

I have a public member function called Command. I want to call this function periodically. I have a global function called Timer to do this. The Timer function periodically executes the function it takes as an argument. I want to call the Timer function in my other public member function named Request by passing the Command function as an argument.

But I am getting this error ----> Namespace::Command: non-standard syntax;use '&' to create a pointer to member

And this error ----> no suitable constructor exists to convert from "uint8_t *(const int &param1, int param2, const int &param3, uint16_t *param4)" to "std::function<void (const int&, int,const int&, uint16_t *)>"

But when I send a global function with the same parametric structure as the Command function as an argument to the Timer function, there is no problem. How can I pass a member function as an argument to a my Timer function ?

My functions as follows;

//////Timer Function//////

void Timer(std::function<void(const int&,int,const int&,uint16_t*)> func)
{
    std::thread([func]() {
        while (true)
        {
            uint16_t s[2]{10,50};
            func(1,5,10,s);         
            std::this_thread::sleep_for(std::chrono::seconds(3));
        }
        }).detach();
}
//////Command Function//////

uint8_t* Namespace::Command(const int& param1, int param2, const int& param3, uint16_t* param4);

//////Request Function//////

vector<Answers>* Namespace::Request() {
Timer(Command); // ERROR ! 
}

Thanks in advance..

Aucun commentaire:

Enregistrer un commentaire