得到错误:' this '对于静态成员函数不可用,即使函数不是静态的

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

本文关键字:函数 静态 静态成员 错误 this      更新时间:2023-10-16

我有功能,在那里我创建新的pthread,然后使用它以后

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功能:

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

showHelp声明

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

当我尝试编译它时,我得到这个错误:

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

sendMessage不声明为static时,这怎么可能?有别的办法吗?

很可能您的sendMessage 在类定义中被声明为静态。对于静态和非静态函数,特定的成员函数定义是无法区分的。您必须查看类定义来区分它们。