2012-03-16 3 views
3

다음은 사용자로부터 가치를 얻으려는 템플릿 매트릭스입니다. 하지만 컴파일 할 때. 오류가 발생했습니다. 왜 오류입니까?C++, 템플릿 인자 에러

SO_template.cpp : 회원 기능 void Matrix<T>::BuildMatrix(std::vector<T, std::allocator<_CharT> >)': SO_template.cpp:44: error: expected에서 ' before "it"

int를 사용하여 클래스를 특수화하면 왜 불만을 나타내지 않습니까? 전체 유형 T에 따라 달라집니다 -

template<class T> 
    class Matrix 
    { 
    private: 
      vector<T> col; 
      int iNumberOfRow; 
      int iNumberOfCol; 
    public: 
    void BuildMatrix(const std::vector<T> stringArray) 
    { 

     std::vector<T>::iterator it= stringArray.begin(); 
     cout<<"Build Matrix irow="<<stringArray.size(); 
     ... 
     ... 
    } 
}; 
+0

가능한 중복 읽기 [어디서 왜 내가 "템플릿"과 "유형 이름"키워드를 넣어해야합니까?] (HTTP : // 유래. com/questions/610245/where-and-why-do-i-have-the-template-and-typename- 키워드) –

답변

6

문제는 std::vector<T>::iterator는 "의존의 형태"는 것입니다. 이 문제를 해결하기 위해 typename와이 접두사, 그렇게 할 선이의

typename std::vector<T>::iterator it= stringArray.begin(); 
+0

약간의 이유는 컴파일이 어떤 std :: 벡터 :: iterator를 참조하면 벡터의 일부 특수화에 대한 정적 멤버 변수가 될 수 있습니다. 따라서 typename은 컴파일러로 하여금 이것이 타입이라고 기대한다고 알립니다. –