J'utilise une bibliothèque fournie par mon professeur pour une affectation de classe, donc je ne peux pas révéler trop de mon code. Voici une partie de l'API:Comment affecter une fonction à un pointeur de fonction qui prend un argument de type void *?
typedef void (*thread_startfunc_t) (void*);
int thread_libinit(thread_startfunc_t func, void *arg);
int thread_create(thread_startfunc_t func, void *arg);
thread_libinit initializes the thread library. A user program should call
thread_libinit exactly once (before calling any other thread functions).
thread_libinit creates and runs the first thread. This first thread is
initialized to call the function pointed to by func with the single
argument arg. Note that a successful call to thread_libinit will not
return to the calling function. Instead, control transfers to func, and
the function that calls thread_libinit will never execute again.
thread_create is used to create a new thread. When the newly created
thread starts, it will call the function pointed to by func and pass it the
single argument arg.
Pour tester, j'ai écrit le code suivant:
void pHello(int i)
{
cout << "Hello from thread " << i << endl;
}
... [In Main] ...
typedef void (*thread_startfunc_t) (void* i);
thread_startfunc_t pSalute = & pHello; //Does not compile
thread_libinit(pSalute,1);
Je reçois l'erreur:
In function `int main(int, char**)`:
error: invalid conversion from `void (*) (int)' to `thread_startfunc_t{aka void(*) (void*)}' [-fpermissive]
Je pensais une variable de pointeur de vide peut pointer vers une variable. Alors pourquoi ne peut-il pointer vers l'int de la fonction pHello? Comment affecter une fonction au pointeur de fonction?
Bien que vous avez ajouté le (presque sûr) devinez pourquoi cette erreur se produit, +1. – davidhigh