2011-07-20 31 views
0

任何评论意识到下面的编译错误。 虽然我的问题类似于其他线程:pthread function from a class,但我仍然无法解决我的问题。我仍然不是那种有指针的familliar,而是C++编写的线程编程。“无效转换”与pthread_create问题

错误

../src/Main.cpp: In function ‘int main(int, char**)’: 
../src/Main.cpp:22: error: invalid conversion from ‘unsigned int* (*)(void*)’ to ‘void* (*)(void*)’ 
../src/Main.cpp:22: error: initializing argument 3 of ‘int pthread_create(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*)’ 
make: *** [src/Main.o] Error 1 

Main.cpp的

#include <process.h> 
#include "ThreadInstance.hpp" 
#include <iostream> 
#include <fstream> 
using namespace std; 

int main(int argc, char** argv) 
{ 
    pthread_mutex_t mutex; 
    int ht1; 
    pthread_t threadId1; 
    pthread_attr_t attr1; 

    pthread_mutex_init(&mutex, NULL); 
    pthread_attr_init(&attr1); 
    pthread_attr_setdetachstate(&attr1, PTHREAD_CREATE_DETACHED); 

    ht1 = pthread_create(&threadId1, 
      &attr1, 
      &ThreadInstance::ThreadEntryPoint, 
      //(void *)readThread); 
      NULL); 

    unsigned long rc = 0; 
    rc = pthread_join(ht1, NULL); 

    return 0; 
} 

ThreadInstance.hpp

#ifndef _SCENE_CLASSIFY_THREAD_H 
#define _SCENE_CLASSIFY_THREAD_H 

#ifndef STDCALL 
#define STDCALL __attribute__((stdcall)) 
#endif 

using namespace std; 

class ThreadInstance 
{ 
    public: 
     ThreadInstance(); 
     ThreadInstance(int camNum); 

     void startUp(); 

     static unsigned STDCALL* ThreadEntryPoint(void* pThis) 
     { 
      //static unsigned __stdcall ThreadEntryPoint(void* pThis) { 
      ThreadInstance *ptr = (ThreadInstance*) pThis; 
      ptr->startUp(); 
      //return 1; // Returns error "invalid conversion from ‘int’ to ‘unsigned int*’" when the function is declared as pointer. 
          // Since returning either 1 or 0, 
          // compile error still occurs. 
          // So this return value should not be the cause. 

      return 0; 
     } 
     ~ThreadInstance(); 
}; 
#endif 

注意:只有在必要部分显示

+0

如果你不知道什么是错的,你怎么知道什么是“必要的”? –

回答

1

ThreadEntryPoint必须retur n void*

该错误指示预期的类型,这是您需要使用的函数指针类型。

+0

谢谢。将返回类型改为void *可以解决这个问题(不管结果是否是我想要的还是不知道的,尽管我现在正在修改某个人的代码)。 – IsaacS

+0

@ user577001:这不是一种解决方法;这是需要的签名。 –

+0

哦,我明白了。这是我想知道的。谢谢@Tomalak Geret'kal – IsaacS

0

启动函数返回void*并且需要void*。您的回报unsigned int*

更改方法以返回指向void的指针。