C++ 获取错误:‘;这’;对于静态成员函数不可用,即使函数不是静态的
我有一个函数,在这里我创建了新的C++ 获取错误:‘;这’;对于静态成员函数不可用,即使函数不是静态的,c++,pthreads,C++,Pthreads,我有一个函数,在这里我创建了新的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::Clien
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);
}
发送消息
功能:
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
声明为static
。我将在一分钟内更新我的帖子。您可能希望在pthread\u create
调用中也使用&Client::sendMessage
,尽管您无法传递成员函数指针。在声明中它将是静态的。。。在定义中使用关键字static是为了其他目的,因此发生此错误时不要在定义中搜索:)有趣的是,您决定省略sendMessage
的声明,其中static
说明符是……好吧……我是个白痴……我花了2个小时寻找可能出错的地方,当然我在头文件x-中将其声明为static)