对';没有匹配的函数调用;pthread_create'; 我使用XCODE和C++来制作一个简单的游戏。 问题在于以下代码: #include <pthread.h> void *draw(void *pt) { // ... } void *input(void *pt) { // .... } void Game::create_threads(void) { pthread_t draw_t, input_t; pthread_create(&draw_t, NULL, &Game::draw, NULL); // Error pthread_create(&input_t, NULL, &Game::draw, NULL); // Error // ... } #包括 空*抽(空*点){ // ... } void*输入(void*pt){ // .... } void游戏::创建线程(void){ pthread\u t draw\u t,input\u t; pthread_create(&draw_t,NULL,&Game::draw,NULL);//错误 pthread_create(&input_t,NULL,&Game::draw,NULL);//错误 // ... }

对';没有匹配的函数调用;pthread_create'; 我使用XCODE和C++来制作一个简单的游戏。 问题在于以下代码: #include <pthread.h> void *draw(void *pt) { // ... } void *input(void *pt) { // .... } void Game::create_threads(void) { pthread_t draw_t, input_t; pthread_create(&draw_t, NULL, &Game::draw, NULL); // Error pthread_create(&input_t, NULL, &Game::draw, NULL); // Error // ... } #包括 空*抽(空*点){ // ... } void*输入(void*pt){ // .... } void游戏::创建线程(void){ pthread\u t draw\u t,input\u t; pthread_create(&draw_t,NULL,&Game::draw,NULL);//错误 pthread_create(&input_t,NULL,&Game::draw,NULL);//错误 // ... },c++,xcode,class,pthreads,C++,Xcode,Class,Pthreads,但是Xcode给了我一个错误:“没有匹配的函数调用'pthread\u create'”。我不知道,因为我已经包含了pthread.h 怎么了 谢谢 使用pthread编译线程是通过提供选项-pthread完成的。 例如编译abc.cpp需要像编译g++-pthread abc.cpp那样编译,否则 给您一个错误,如对pthread\u create collect2:ld返回1退出状态的未定义引用。必须有类似的方法来提供pthread选项。您正在传递一个成员函数指针(即,&Game::draw

但是Xcode给了我一个错误:“
没有匹配的函数调用'pthread\u create'
”。我不知道,因为我已经包含了
pthread.h

怎么了


谢谢

使用pthread编译线程是通过提供选项
-pthread
完成的。 例如编译abc.cpp需要像编译
g++-pthread abc.cpp那样编译,否则

给您一个错误,如
pthread\u create collect2:ld返回1退出状态的未定义引用。必须有类似的方法来提供pthread选项。

您正在传递一个成员函数指针(即,
&Game::draw
),其中需要纯函数指针。您需要使该函数成为类静态函数


编辑添加:如果需要调用成员函数(很可能),则需要创建一个类静态函数,将其参数解释为
游戏*
,然后在该类上调用成员函数。然后,将
this
作为
pthread_create()
的最后一个参数传递,正如Ken所说,作为线程回调传递的函数必须是(void*)(*)(void*)类型的函数

您仍然可以将此函数作为类函数包含,但必须将其声明为静态。对于每种螺纹类型(如draw),您可能需要一个不同的螺纹

例如:

class Game {
   protected:
   void draw(void);
   static void* game_draw_thread_callback(void*);
};

// and in your .cpp file...

void Game::create_threads(void) {
   //  pass the Game instance as the thread callback's user data
   pthread_create(&draw_t, NULL, Game::game_draw_thread_callback, this);
}

static void* Game::game_draw_thread_callback(void *game_ptr) {
   //  I'm a C programmer, sorry for the C cast.
   Game * game = (Game*)game_ptr;

   //  run the method that does the actual drawing,
   //  but now, you're in a thread!
   game->draw();
}

你的代码给了我这个错误(不是“没有匹配函数…”):“必须调用非静态成员函数的引用”谢谢,我经常使用C。在C语言中,它只通过调用函数来工作;)