2016-06-19 6 views
0

私はコードの下にテストしようとしているが、コンパイルエラー取得しています「エラー 'int型の前に期待される主な表現」
テンプレートメンバ関数の呼び出し -</p> <blockquote> <p>TemplateTest.cpp:44:13: error:expected primary-expression before 'int'<br/>         h_.handle<int>(i);</p> </blockquote> <p>ビルドコマンド:::

g++ -c -std=c++11 -g -O2 -Wall -Werror TemplateTest.cpp -o TemplateTest.o

行の下
#include <iostream> 
using namespace std; 

enum PROTOCOL { 
    PROTO_A, 
    PROTO_B 
}; 

// ----- HandlerClass ------- 
template <PROTOCOL protocol> 
class Handler { 
public: 
    template <class TMsg> 
    bool handle(const TMsg&) { 
     return false; 
    } 
}; 

template <> 
template <class TMsg> 
bool Handler<PROTO_A>::handle(const TMsg&) { 
    cout << "PROTO_A handler" << endl; 
    return true; 
} 

template <> 
template <class TMsg> 
bool Handler<PROTO_B>::handle(const TMsg&) { 
    cout << "PROTO_B handler" << endl; 
    return true; 
} 

// ----- DataClass ------ 
template <PROTOCOL protocol> 
struct Data { 
    typedef Handler<protocol> H; //select appropriate handler 
    H h_; 
    int i; 
    Data() : i() {} 

    void f() { 
      h_.handle<int>(i); //***** <- getting error here 
    } 
}; 

int main() { 
    Data<PROTO_A> b; 
    b.f(); 
    return 0; 
} 
+0

'template'内部に別のテンプレート1が必要' template'句を呼び出します。あなたの特定の例では、 'h_.template handle (i);' –

+0

THANKS W.F.これはうまくいった。 :)。しかし、なぜ?関連する参照番号 – ashwin929

+0

を実際に指摘することができますか?コンパイラは "<"文字が少ない演算子であるかどうか、またはテンプレート呼び出しの開始点であるかどうかをコンパイラが確かめることはできません...関連:http:// stackoverflow .com/questions/7397934/call-template-function-within-template-class –

答えて

関連する問題