2012-03-16 133 views
3

以下是我想通過從用戶獲取值來構建的模板矩陣。 但是當我編譯它。我正在低於錯誤。爲什麼錯誤?C++,模板參數錯誤

SO_template.cpp: 在成員函數void Matrix<T>::BuildMatrix(std::vector<T, std::allocator<_CharT> >)': SO_template.cpp:44: error: expected;'之前「它」

如果我使用int專門化我的類它不會抱怨爲什麼?

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-to-put-the-template-and-typename-keywords) – 2012-03-16 13:18:20

回答

6

問題是std::vector<T>::iterator是一種「依賴型」 - 全類型取決於T。與typename前綴,它解決的問題,所以使讀取線的

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

稍微重申一下,這是因爲compile不知道std ::向量 ::迭代器會引用它,例如,它可能是一個向量的一些專業化的靜態成員變量。所以typename讓編譯器知道你期望它是一個類型。 – 2012-03-16 11:03:27