2011-08-04 34 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指針傳遞給一個靜態/全局線程啓動例程,將該指針轉換爲適當的類類型,並用它來調用將在另一個線程中執行的實際實例方法。