2011-08-04 33 views
0

使用C++静态编译错误模板

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. 
    } 
} 

我得到如下错误 错误1错误C2352:“ThreadWrapper :: ExecuteThread”:非静态成员函数

我的非法调用我正在编译VS2010。

任何人可以帮助我在这里如何清除错误。

谢谢!

+0

你有一个错字,你的意思是'pWrapper-> ExecuteThread()',正确吗? –

+1

这是一个明显的错误,我不得不问,你甚至检查过编译器错误试图告诉你什么吗? –

+0

这太明显了吗?你有任何问题要了解这个错误? – neuront

回答

0

你错过了 “>” 这是

pWrapper->ExecuteThread() 

pWrapper-ExecuteThread() 
2

你错过了>在通话。你想return pWrapper->ExecuteThread();

3

的问题是,你的错误行

return pWrapper-ExecuteThread(); // Error here. 

错过的>;它应该是

return pWrapper->ExecuteThread(); // Error here. 

因为它试图执行减法,所以出现了这样一个奇怪的编译错误;指针pWrapper被视为一个整数,通过调用ExecuteThread()(产生一个int)返回的值将从该值中减去。但ExecuteThread()既不是全局函数也不是静态成员函数 - 因此编译器会抱怨。

0

你不能调用静态成员函数与语法。尝试这样做,而不是执行以下操作:

static_cast<ThreadWrapper*>(pParam)->ExecuteThread(); 

也许是多余的,但一个解释:那是入口点线程不能实例方法的功能,它们必须是文件作用域的函数或静态方法。常见的习惯用法是将一个void指针传递给一个静态/全局线程启动例程,将该指针转换为适当的类类型,并用它来调用将在另一个线程中执行的实际实例方法。