2016-05-31 6 views
4

私はC++でPointクラスを作成しており、これにテンプレートを使用しています。しかし、私は理解していないコンパイルエラーがあります。私は、問題の最小限の例を書いた:ネストされたテンプレート: "before primary-expression expected ')" "

#include <array> 
#include <vector> 
#include <iostream> 
template <typename T, int DIM> 
class Point 
{ 
    private: 
     std::array<T, DIM> values; 

    public: 
     template <int ROW> 
     T get() 
     { 
      return values.at(ROW); 
     }; 
}; 

template <typename T> 
class Field 
{ 
    public: 
     T print(std::vector<Point<T, 3> >& vec) 
     { 
      for (auto it : vec) 
      { 
       T bla = it.get<1>(); // the error line 27 
      } 
     }; 
}; 

int main(int argc, 
     char* argv[]) 
{ 
    Point<double, 3> p; 
    double val = p.get<1>(); 
    std::cout << val << std::endl; 

    Field<int> f; 
    std::vector<Point<int, 3> > vec; 
    f.print(vec); 

    return 0; 
} 

私は

g++ main2.cpp -std=c++11 

でコンパイルすると出力が

main2.cpp: In member function ‘T Field<T>::print(std::vector<Point<T, 3> >&)’: 
main2.cpp:27:33: error: expected primary-expression before ‘)’ token 
      T bla = it.get<1>(); 
           ^
main2.cpp: In instantiation of ‘T Field<T>::print(std::vector<Point<T, 3> >&) [with T = int]’: 
main2.cpp:41:16: required from here 
main2.cpp:27:27: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to binary ‘operator<’ 
      T bla = it.get<1>(); 

エラーが発生し、それを解決するためにどのように、なぜ誰かが知っていますか?

ありがとうございます。

答えて

3

it.get<1>()は、テンプレートパラメータに依存しているので、あなたはそれを正しく解析することができるようにgetがテンプレートで、コンパイラに指示する必要があり:

さらに
T bla = it.template get<1>(); 

を、あなたはそれから何も返さないprintたとえその宣言がTを返すべきだと言っています。

templateキーワードの詳細については、thisを参照してください。あなたは、彼らがしているクラスはテンプレートクラスそのものである場合には、テンプレートのメンバ関数にアクセスするためにtemplateキーワードが必要

T bla = it.template get<1>(); // the error line 27 

:へ

1

変更ライン

T bla = it.get<1>(); // the error line 27 

関連する問題