Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error: ‘this’ is unavailable for static member functions even when function is not static

Tags:

c++

pthreads

I have function, where I create new pthread and then work with it later

void Client::initialize(Client * c) {
//some unimportant code here
    pthread_t thread;
    pthread_create(&thread, NULL,
            c->sendMessage, (void *) fd);
//some unimportant code here
}

Client::Client() {
    initialize(this);
}

sendMessage function:

void * Client::sendMessage(void *threadid) {
    //unimportant code here      
    this->showHelp();
    //unimportant code here
    return NULL;
}

declaration of showHelp

void Client::showHelp() {
    //some code
}

When I try to compile it, I get this error:

g++ -Wall -pedantic -Wno-long-long -O0 -ggdb -pthread -lncurses -g -c ./Client.cpp
./Client.cpp: In static member function ‘static void* Client::sendMessage(void*)’:
./Client.cpp:244:13: error: ‘this’ is unavailable for static member functions
make: *** [Client.o] Error 1

How is that possible, when sendMessage is not declared as static ? Is there any way around?

like image 468
Martin Dvoracek Avatar asked Nov 16 '25 10:11

Martin Dvoracek


1 Answers

Most likely your sendMessage is declared as static in class definition. Specific member function definitions are indistinguishable for static and non-static functions. You have to look at class definition to tell them apart.

like image 196
AnT Avatar answered Nov 19 '25 00:11

AnT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!