First I have created a custom type as follows:
typedef void* (*FUNCPTR)(void*);
Then, I created a Base Class called P
.
class P
{
...
public:
virtual void job() = 0; // Pure virtual function
void start()
{
create((FUNCPTR) &P::job);
}
...
};
(create
method signature accepts void *(*__start_routine)(void *)
.)
Next, I create a Derived Class called PD
where I have implemented the pure virtual function job()
.
When trying to compile the following error is raised:
/usr/bin/ld: /tmp/ccNwL4X6.o: in function P::start()': undefined reference to P::job()
P::job()
is not being called, instead PD::job()
, and it is causing the error?Edit 1. Minimal reproducible example.
#include <iostream>
#include <pthread.h>
using namespace std;
typedef void* (*THREADPTR)(void*);
class P
{
protected:
pthread_t thread;
public:
virtual void job() = 0;
void start()
{
cout << "P::start()" << endl;
pthread_create(&thread, NULL, (THREADPTR) &P::job, (void*) this);
}
};
class PD: public P
{
public:
void job()
{
cout << "PD::job()" << endl;
}
};
int main()
{
P *pd;
pd = new PD();
pd->start();
}
Edit 2. Provide desired functionality.
I would love to code only the logic for the job()
function for in each class that extends the abstract one.
Edit 3. Detailed information about the create()
function.
The create()
function was oversimplified because not so much attention to it was expected. But was not the case, it is a fundamental part of the problem. With that being said, the actual function is this:
extern int pthread_create (pthread_t *__restrict __newthread,
const pthread_attr_t *__restrict __attr,
void *(*__start_routine) (void *),
void *__restrict __arg) __THROWNL __nonnull ((1, 3));
as defined in pthread.h
The minimal reproducible example was modified to account for this.
Edit 4. Question marked as duplicate.
The question marked as duplicate does not match with the intentions that were laid out here. The solution was tried without success giving the parser already the error: virtual void P::job() argument of type "void (P::*)()" is incompatible with parameter of type "void *(*)(void *)"C/C++(167)
Now that it's been clarified that create
is infact pthread_create
, you can have
class P
{
static void* do_job(void* self)
{
static_cast<P*>(self)->job();
return nullptr;
}
pthread_t thread;
public:
virtual void job() = 0;
void start()
{
pthread_create(&thread, nullptr, &P::do_job, this);
cout << "P::start()" << endl;
}
};