Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ pthread_create template函数——静态转换模板类_C++_Templates_Casting_Callback_Pthreads - Fatal编程技术网

C++ pthread_create template函数——静态转换模板类

C++ pthread_create template函数——静态转换模板类,c++,templates,casting,callback,pthreads,C++,Templates,Casting,Callback,Pthreads,我不知道是否需要比下面的代码更多的信息,但是如果需要更多的信息,请说出来,我将发布剩余的代码。编译时,我遇到以下错误: g++ -c -pipe -O2 -Wall -W -I../../../../QtSDK/Desktop/Qt/4.8.0/gcc/mkspecs/linux-g++ -I. -o main.o main.cpp In file included from main.cpp:4: TimerManager.h: In function 'void* create_pthre

我不知道是否需要比下面的代码更多的信息,但是如果需要更多的信息,请说出来,我将发布剩余的代码。编译时,我遇到以下错误:

g++ -c -pipe -O2 -Wall -W  -I../../../../QtSDK/Desktop/Qt/4.8.0/gcc/mkspecs/linux-g++ -I. -o main.o main.cpp
In file included from main.cpp:4:
TimerManager.h: In function 'void* create_pthread(void*)':
TimerManager.h:17: error: expected nested-name-specifier before 'TimerManager'
TimerManager.h:17: error: expected '(' before 'TimerManager'
TimerManager.h:17: error: expected ';' before 'TimerManager'
make: *** [main.o] Error 1

为了消除这些错误,我需要在下面更改什么


模板
void*创建线程(void*数据)
{
typename TimerManager*tm=静态转换(数据);
返回数据;
}
...
模板
类计时器管理器{
...
};
...
模板
TimerManager::TimerManager():
m_bRunning(错),
m_bGo(假),
m_lMinSleep(0)
{
int mutex\u creation=pthread\u mutex\u init(&m\u tGoLock,NULL);
如果(互斥创建!=0){
抛出TimerManager::TimerError(std::string(“未能创建互斥”);
}
int mutex_cond_creation=pthread_cond_init(&m_tGoLockCondition,NULL);
如果(互斥条件创建!=0){
抛出TimerManager::TimerError(std::string(“未能创建条件互斥”);
返回;
}
int-thread\u creation=pthread\u create(&m\u tTimerThread,NULL,create\u pthread,this);
如果(线程创建!=0){
抛出TimerManager::TimerError(std::string(“未能创建线程”);
返回;
}
m_bRunning=真;
}

我认为问题在于,考虑到声明的顺序,
timermager
类模板在定义
create\u pthread
之前没有声明过。因此,编译器会报告一个错误,因为
timermager
不在范围内。对函数重新排序应该可以解决这个问题

此外,行中不需要
typename

typename TimerManager<Object> *tm = static_cast<TimerManager<Object> *>(data);
typename timermager*tm=static_cast(数据);
只有在访问
TimerManager
中的嵌套类型时,
typename
才是必需的。你应该能够删除它没有任何问题


希望这有帮助

哪一行是第17行(有错误的那一行?)typename TimerManager*tm=static_cast(数据);等待-在
create_pthread
函数之前是否声明了
timermager
?否,请忘记转发声明。一秒钟前才发现。发布解决方案后,我会给你要点。直接的错误是在类型前面有多余的
typename
。它既不需要也不允许。另外,请注意,只能将函数C linkag e用作
pthread_create()
(即声明为
extern“C”
)的参数。函数模板不能声明为具有C链接。
typename TimerManager<Object> *tm = static_cast<TimerManager<Object> *>(data);