C++ C++;使用模板时发生静态编译错误
我有C++ C++;使用模板时发生静态编译错误,c++,templates,C++,Templates,我有 模板 类线程包装器 { 公众: 静态int-ThreadRoutineFunction(void*pParam); int ExecuteThread(); 螺纹包装器(螺纹池*pPool); }; 模板 int ThreadWrapper::ThreadRoutineFunction(void*pParam) { 螺纹护套*pWrapper=重新解释铸件(pParam); 如果(pwraper!=NULL) { 返回pWrapper-ExecuteThread();//此处出错。 } 返
模板
类线程包装器
{
公众:
静态int-ThreadRoutineFunction(void*pParam);
int ExecuteThread();
螺纹包装器(螺纹池*pPool);
};
模板
int ThreadWrapper::ThreadRoutineFunction(void*pParam)
{
螺纹护套*pWrapper=重新解释铸件(pParam);
如果(pwraper!=NULL)
{
返回pWrapper-ExecuteThread();//此处出错。
}
返回0;
}
模板
ThreadWrapper::ThreadWrapper(线程池*pPool)
{
ThreadWrapper::mpthreadpool=pPool;
m_tbbThread=new tbb::tbb_thread(&(ThreadWrapper::ThreadRoutineFunction),this);
如果(m_tbbThread->native_handle()==0)
{
删除m_tbb线程;
m_tbbThread=NULL;
//TODO:在此处抛出execption或引发断言。
}
}
我得到的错误如下
错误1错误C2352:“ThreadWrapper::ExecuteThread”:非法调用非静态成员函数
我正在VS2010上编译
这里有谁能帮我清除错误吗
谢谢 您错过了通话中的>。您需要
返回pwraper->ExecuteThread()代码>您错过了通话中的>。您需要返回pwraper->ExecuteThread()代码>问题在于您的错误行
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.
}
}
错过一个
;应该是
return pWrapper-ExecuteThread(); // Error here.
由于它试图执行减法运算,所以会出现如此奇怪的编译错误;指针pwraper
被视为一个整数,从中减去通过调用ExecuteThread()
返回的值(这将产生一个int
)。但是,ExecuteThread()
既不是全局函数,也不是静态成员函数-因此编译器会抱怨。问题在于错误行
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.
}
}
错过一个
;应该是
return pWrapper-ExecuteThread(); // Error here.
由于它试图执行减法运算,所以会出现如此奇怪的编译错误;指针pwraper
被视为一个整数,从中减去通过调用ExecuteThread()
返回的值(这将产生一个int
)。但是,ExecuteThread()
既不是全局函数,也不是静态成员函数-因此编译器会抱怨。您缺少“>”
它是
不是
你错过了“>”
它是
不是
不能用这种语法调用静态成员函数。请尝试执行以下操作:
pWrapper-ExecuteThread()
static_cast(pParam)->ExecuteThread();
可能是多余的,但有一个解释:作为线程入口点的函数不能是实例方法,它们必须是文件作用域函数或静态方法。常用的习惯用法是向静态/全局线程启动例程传递一个void指针,将该指针强制转换为正确的类类型,并使用它调用将在另一个线程中执行的实际实例方法。不能使用该语法调用静态成员函数。请尝试执行以下操作:
pWrapper-ExecuteThread()
static_cast(pParam)->ExecuteThread();
可能是多余的,但有一个解释:作为线程入口点的函数不能是实例方法,它们必须是文件作用域函数或静态方法。常见的习惯用法是将一个空指针传递给静态/全局线程启动例程,将该指针强制转换为正确的类类型,并使用它调用将在另一个线程中执行的实际实例方法。您有一个拼写错误,您的意思是让pwraper->ExecuteThread()
,对吗?这是一个非常明显的错误,我不得不问,你有没有检查过编译器错误想告诉你什么?这是不是太明显了?你理解这个错误有什么问题吗?因为编译器抱怨有一个调用,我假设SO的格式化使用了'>'字符。你有一个打字错误,你的意思是让pwraper->ExecuteThread()
,对吗?这是一个非常明显的错误,我不得不问,你有没有检查过编译器错误想告诉你什么?这是不是太明显了?您理解这个错误有什么问题吗?因为编译器抱怨调用,所以我假设SO的格式设置错误了'>'字符。