c++静态编译错误使用模板
C++ static compile error using templates
我有
template < typename threadFuncParamT >
class ThreadWrapper
{
public:
static int ThreadRoutineFunction(void* pParam);
int ExecuteThread();
ThreadWrapper(ThreadPool<threadFuncParamT> *pPool);
};
template<typename threadFuncParamT>
int ThreadWrapper<threadFuncParamT>::ThreadRoutineFunction(void* pParam)
{
ThreadWrapper<threadFuncParamT> *pWrapper = reinterpret_cast<ThreadWrapper<threadFuncParamT>*>(pParam);
if(pWrapper != NULL)
{
return pWrapper-ExecuteThread(); // Error here.
}
return 0;
}
template < typename threadFuncParamT >
ThreadWrapper<threadFuncParamT>::ThreadWrapper(ThreadPool<threadFuncParamT> *pPool)
{
ThreadWrapper<threadFuncParamT>::m_pThreadPool = pPool;
m_tbbThread = new tbb::tbb_thread(&(ThreadWrapper<threadFuncParamT>::ThreadRoutineFunction), this);
if (m_tbbThread->native_handle() == 0)
{
delete m_tbbThread;
m_tbbThread = NULL;
// TODO: throw execption here or raise assert.
}
}
我得到的错误如下C2352: 'ThreadWrapper::ExecuteThread':非法调用非静态成员函数
我正在VS2010上编译。
这里有谁能帮我清除这个错误吗?
谢谢!
问题是你的错误行
return pWrapper-ExecuteThread(); // Error here.
缺失一个>
;应该是
return pWrapper->ExecuteThread(); // Error here.
你得到这样一个奇怪的编译错误,因为它试图执行减法;指针pWrapper
被视为一个整数,调用ExecuteThread()
返回的值(它产生一个int
)从中减去。然而,ExecuteThread()
既不是全局函数,也不是静态成员函数——因此编译器会报错。
您错过了电话中的>。你想要return pWrapper->ExecuteThread();
你错过了">"这是
pWrapper->ExecuteThread()
不pWrapper-ExecuteThread()
不能用这种语法调用静态成员函数。试着这样做:
static_cast<ThreadWrapper*>(pParam)->ExecuteThread();
可能是多余的,但是解释一下:作为线程入口点的函数不能是实例方法,它们必须是文件作用域函数或静态方法。常用的习惯用法是将一个void指针传递给静态/全局线程启动例程,将该指针强制转换为正确的类类型,并使用它来调用将在另一个线程中执行的实际实例方法。
相关文章:
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 尝试使用 std::vector<std::thread时出现静态断言失败错误>
- 尝试了解在导入的静态方法上使用删除方法时的错误
- 使用静态库与 std::jthread (g++-10) 的分段错误
- 静态成员函数使用相同的名称时出现模板类型名称错误
- Clang 8 带有静态 constexpr 和数组的链接器错误 - 错误是什么以及如何解决它?
- 为什么传递非静态成员函数会导致编译错误?
- 在静态库中使用输出迭代器时出现链接器错误
- 线程时访问静态映射时出现隔离错误
- 错误: 无效使用非静态数据成员"应用程序::应用程序构造函数"
- 静态库单独使用很好,但在引用时会抛出错误
- 静态链接启用 PIC 的库时出现链接器 PIC 错误
- 漂亮的计数器习语的错误或格式错误的静态订单惨败?
- 如何解决C++中声纳库贝静态代码分析错误"Explicitly define the missing copy constructor, move constructor .."
- 静态链接库时,收到链接器错误:找不到 -lgcc_s
- 错误:静态断言失败:std ::线程参数必须在转换为rvalues后不可行
- 链接器错误:静态链接提升序列化库
- C++程序上的执行错误:静态可执行文件调用 DLL 库
- 未定义已声明的错误c++静态方法
- "Undefined reference to" 错误/静态成员变量 C++/Qt