pthread_create 模板函数——静态转换模板类

发布于 2025-01-06 06:17:35 字数 1617 浏览 1 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小镇女孩 2025-01-13 06:17:35

我认为问题在于,考虑到声明的顺序,在定义 create_pthread 之前尚未声明 TimerManager 类模板。因此,编译器会报告错误,因为 TimerManager 不在范围内。重新排序功能应该可以解决这个问题。

此外,您不需要在 typename 行中使用 typename

typename TimerManager<Object> *tm = static_cast<TimerManager<Object> *>(data);

仅当您访问 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 of create_pthread. As a result, the compiler reports an error because TimerManager isn't in scope. Reordering the functions should fix that.

Also, you don't need a typename in the line

typename TimerManager<Object> *tm = static_cast<TimerManager<Object> *>(data);

typename is only necessary if you are accessing a nested type inside of TimerManager<Object>. You should be able to remove it without any problems.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文