Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot convert ‘void* (Network::*)(void*)’ to ‘void* (*)(void*)’

Tags:

c++

void

I'm a beginning C++ programmer and I'm programming on a Linux machine.

I got this error:

cannot convert ‘void* (Network::*)(void*)’ to ‘void* (*)(void*)’ for argument ‘3’ to ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)

It is comming from this line:

pthread_create(&thread_id,0,&Network::SocketHandler, (void*)csock );

The function I'm trying to call is:

void* Network::SocketHandler(void* lp)

I declared both functions in the header file as private.

Do any of you see what I'm doing wrong?

like image 262
mschuurmans Avatar asked Feb 18 '23 21:02

mschuurmans


1 Answers

You are using a member function pointer where a regular function pointer is expected. A member function has an implicit extra parameter: this. pthread_create does not account for that.

You will have to make the function static to be able to use it with pthread_create. You can then use the void* parameter to pass along what would otherwise be the this pointer.

Personally, I would just ditch pthreads in favor of C++11 std::thread, or boost::thread if you don't have access to a C++11 implementation.

like image 154
K-ballo Avatar answered Feb 28 '23 09:02

K-ballo