I've got a class named tmc, which now contains (among other things, which are not relevant here) a private constructor, a static factory method, and an instance method named ReadLoop (void*)
:
extern "C" {
#include <pigpiod_if2.h>
}
class tmc {
public:
static tmc* Initialize ();
static int main ();
private:
void *ReadLoop (void *params);
tmc ();
pthread_t *tmc_receiver_reader;
};
tmc::tmc ()
: tmc_receiver_reader (start_thread (tmc::ReadLoop, NULL))
{
}
void* tmc::ReadLoop (void *params)
{
return params;
}
tmc* tmc::Initialize ()
{
tmc* instance = new tmc ();
return instance;
}
int tmc::main ()
{
return (tmc::Initialize ()) == NULL ? 0 : 1;
}
The issue is now the following: How shall I use the ReadLoop
as a function pointer for use with the start_thread ()
function contained in pigpiod_if2
? The code shown here doesn't compile because of the following error:
error: invalid use of non-static member function ‘void* tmc::ReadLoop(void*)’
tmc_receiver_reader (start_thread (tmc::ReadLoop, NULL))
I've seen several questions here at SO with the same error message, but none of them was about a pointer to a non-static member method given to a C function. Please note that even if the object created here is a singleton, I can't make ReadLoop ()
static. I'm using the g++ 6.5.0 on Raspbian Buster. Thank you.
There is simply no way to use a non-static class method where a standalone function is expected. They are not compatible.
You are going to have to use a static or non-member proxy function instead. Fortunately, start_thread()
allows you to pass a user-defined parameter to the function, so you can use that to pass the this
pointer of your tmc
instance, eg:
class tmc {
...
private:
static void* ReadLoopProxy(void *params);
void* ReadLoop();
tmc ();
pthread_t *tmc_receiver_reader;
};
tmc::tmc ()
: tmc_receiver_reader (start_thread (tmc::ReadLoopProxy, this))
{
}
void* tmc::ReadLoopProxy(void *params)
{
return static_cast<tmc*>(params)->ReadLoop();
}
void* tmc::ReadLoop()
{
return NULL;
}
...