我有一個cThread類C++ API,並且此方法來創建一個線程:C++指針的函數作爲參數
void cThread::start(void(*a_function)(void), CThreadPriority a_level);
我已經做了類和init()方法來啓動螺紋和updateHaptics()方法由線程執行:
void EntryClass::init()
{
typedef void (EntryClass::*method)();
method p;
p = &EntryClass::updateHaptics;
// create a thread which starts the main haptics rendering loop
cThread* hapticsThread = new cThread();
hapticsThread->start(p, CTHREAD_PRIORITY_HAPTICS);
}
void EntryClass::updateHaptics(void)
{
// ...
}
我的問題是要通過updateHaptics()方法的一個參數cThread ::開始()方法。
我得到這個錯誤:
1>EntryClass.cpp(55): error C2664: 'void chai3d::cThread::start(void (__cdecl *)(void *),const chai3d::CThreadPriority,void *)' : impossible de convertir l'argument 1 de 'method' en 'void (__cdecl *)(void)'
REM:我的Windows 8/Visual Studio中
updateHaptics必須一個函數或一個靜態方法。 – jsantander
不幸的是,該API基本上被打破了,因爲沒有辦法將任何數據傳遞給線程,因此也沒辦法提供一個類對象來調用成員函數。我會切換到標準的線程庫(或者Boost,如果你被一個古老的編譯器困住了)。 –
這是因爲,成員類的函數名稱是由類的名稱裝飾的。靜態功能沒有裝飾。 – LukeCodeBaker