pthread_create 模板函数——静态转换模板类
我不知道是否需要比下面的代码更多的信息,但如果需要更多信息,请直接说出来,我将发布剩余的代码。编译时出现以下错误:
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
我需要更改以下内容才能消除这些错误?
template<class Object>
void *create_pthread(void *data)
{
typename TimerManager<Object> *tm = static_cast<TimerManager<Object> *>(data);
return data;
}
...
template<class CallObject>
class TimerManager {
...
};
...
template<class CallObject>
TimerManager<CallObject>::TimerManager() :
m_bRunning(false),
m_bGo(false),
m_lMinSleep(0)
{
int mutex_creation = pthread_mutex_init(&m_tGoLock, NULL);
if(mutex_creation != 0) {
throw TimerManager::TimerError(std::string("Failed to create mutex"));
}
int mutex_cond_creation = pthread_cond_init(&m_tGoLockCondition, NULL);
if(mutex_cond_creation != 0) {
throw TimerManager::TimerError(std::string("Failed to create condition mutex"));
return;
}
int thread_creation = pthread_create(&m_tTimerThread, NULL, create_pthread<CallObject>, this);
if(thread_creation != 0) {
throw TimerManager::TimerError(std::string("Failed to create thread"));
return;
}
m_bRunning = true;
}
I don't know if more information is needed than the code below, but if more is needed just say so and I will post the remaining code. When compiling I am getting the following error:
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
What do I need to change below to get rid of these errors?
template<class Object>
void *create_pthread(void *data)
{
typename TimerManager<Object> *tm = static_cast<TimerManager<Object> *>(data);
return data;
}
...
template<class CallObject>
class TimerManager {
...
};
...
template<class CallObject>
TimerManager<CallObject>::TimerManager() :
m_bRunning(false),
m_bGo(false),
m_lMinSleep(0)
{
int mutex_creation = pthread_mutex_init(&m_tGoLock, NULL);
if(mutex_creation != 0) {
throw TimerManager::TimerError(std::string("Failed to create mutex"));
}
int mutex_cond_creation = pthread_cond_init(&m_tGoLockCondition, NULL);
if(mutex_cond_creation != 0) {
throw TimerManager::TimerError(std::string("Failed to create condition mutex"));
return;
}
int thread_creation = pthread_create(&m_tTimerThread, NULL, create_pthread<CallObject>, this);
if(thread_creation != 0) {
throw TimerManager::TimerError(std::string("Failed to create thread"));
return;
}
m_bRunning = true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于,考虑到声明的顺序,在定义
create_pthread
之前尚未声明TimerManager
类模板。因此,编译器会报告错误,因为TimerManager
不在范围内。重新排序功能应该可以解决这个问题。此外,您不需要在
typename
行中使用typename
仅当您访问
TimerManager
希望这有帮助!
I think the problem is that, given the ordering you have of the declarations, the
TimerManager
class template hasn't been declared prior to your definition ofcreate_pthread
. As a result, the compiler reports an error becauseTimerManager
isn't in scope. Reordering the functions should fix that.Also, you don't need a
typename
in the linetypename
is only necessary if you are accessing a nested type inside ofTimerManager<Object>
. You should be able to remove it without any problems.Hope this helps!