2012-02-16 15 views
1

以下のコードより多くの情報が必要かどうかはわかりませんが、もっと必要な場合はそれを言うだけで残りのコードを投稿します。私は次のエラーを取得していますコンパイルする場合:pthread_createテンプレート関数 - スタティックテンプレートクラスのキャスト

g++ -c -pipe -O2 -Wall -W -I../../../../QtSDK/Desktop/Qt/4.8.0/gcc/mkspecs/linux-g++ -I. -o main.o main.cpp 
In file included from main.cpp:4: 
TimerManager.h: In function 'void* create_pthread(void*)': 
TimerManager.h:17: error: expected nested-name-specifier before 'TimerManager' 
TimerManager.h:17: error: expected '(' before 'TimerManager' 
TimerManager.h:17: error: expected ';' before 'TimerManager' 
make: *** [main.o] Error 1 

私は、これらのエラーを取り除くために、以下の変更する必要がありますか?


template<class Object> 
void *create_pthread(void *data) 
{ 
    typename TimerManager<Object> *tm = static_cast<TimerManager<Object> *>(data); 
    return data; 
} 

... 

template<class CallObject> 
class TimerManager { 
    ... 
}; 

... 

template<class CallObject> 
TimerManager<CallObject>::TimerManager() : 
    m_bRunning(false), 
    m_bGo(false), 
    m_lMinSleep(0) 
{ 
    int mutex_creation = pthread_mutex_init(&m_tGoLock, NULL); 
    if(mutex_creation != 0) { 
    throw TimerManager::TimerError(std::string("Failed to create mutex")); 
    } 

    int mutex_cond_creation = pthread_cond_init(&m_tGoLockCondition, NULL); 
    if(mutex_cond_creation != 0) { 
    throw TimerManager::TimerError(std::string("Failed to create condition mutex")); 
    return; 
    } 

    int thread_creation = pthread_create(&m_tTimerThread, NULL, create_pthread<CallObject>, this); 
    if(thread_creation != 0) { 
    throw TimerManager::TimerError(std::string("Failed to create thread")); 
    return; 
    } 
    m_bRunning = true; 
} 
+0

どの行が17行目(エラーのあるものは?) – templatetypedef

+0

typename TimerManager * tm = static_cast *>(データ); –

+0

Wait - あなたの 'create_pthread'関数の前に' TimerManager'が宣言されていますか? – templatetypedef

答えて

2

私はこの問題は、宣言のあなたが持っている発注与えられ、TimerManagerクラステンプレートは、前create_pthreadのあなたの定義に宣言されていない、ということだと思います。結果として、TimerManagerが範囲外であるため、コンパイラはエラーを報告します。関数を並べ替えると、それを修正する必要があります。

また、あなたがTimerManager<Object>の内側にネストされたタイプにアクセスしている場合はラインでtypename

typename TimerManager<Object> *tm = static_cast<TimerManager<Object> *>(data); 

typenameが必要なだけである必要はありません。あなたは問題なくそれを取り除くことができるはずです。

希望すると便利です。

関連する問題