0

I have a function pointer that I am trying to pass along to a class method, where pthread_create will be invoked to pass along that parameter. But I am getting some errors and not sure where I am suppose to go from here.

void (*FuncPointer)(void*);
FuncPointer = random_function;
ThreadPool.Task(FuncPointer);


int IOThreadPool::Task(void* (*FuncPointer)(void*))
{
    pthread_t NewThread;

    int rc = pthread_create(&NewThread, NULL, FuncPointer, (void *) (intptr_t) IOThreadPool::Threads.size() + 1);

main.cpp:57:29: error: invalid conversion from ‘void* (*)()’ to ‘void (*)(void*)’ [-fpermissive]

Please explain why even though I am passing it as void* (*)(void*), I get void* (*)(). I am very confused and my head has begun to hurt!

Thanks

1
  • could you provide random_function too? It has the correct signature? Commented Jul 22, 2016 at 14:33

1 Answer 1

1
invalid conversion from ‘void* (*)()’ to ‘void (*)(void*)’

You forgot to show us the declaration of random_function, but judging from the error message, it appears to be

void* ranfom_function()

Since the signature of the function does not match the signature of FuncPointer (ranfom_function lacks the argument , and return type is different), you get the error.


Another bug, that is not shown by the error: The return type of FuncPointer does not match the return type of the argument accepted by IOThreadPool::Task (void vs void*).


Solution: Declare instad

void* ranfom_function(void*)

and

void* (*FuncPointer)(void*)
Sign up to request clarification or add additional context in comments.

1 Comment

You're the man. This was it. Thank you for explaining that to me... You also just shed a new light on this concept for me. I understand now why it wasn't working. Cheers to you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.