我試圖創建一個模板函數接受任何類型T的向量參數(原始現在),並打印出它的內容:語法聲明一個函數模板
template<class T>
void displayContents(const vector<T>& data)
{
vector<T>::const_iterator i;
i=data.begin();
for(; i!=data.end(); i++){
cout<<*i<endl;
}
}
的錯誤信息是:
In function 'void displayContents(const std::vector >&)': error: expected ';' before 'i' | error:'i' was not declared in this scope === Build finished: 2 errors, 0 warnings ===
我是否忽略了語法錯誤?
的問題是,'const_iterator'是*依賴名稱* - 你可以在這裏閱讀更多關於它:http://stackoverflow.com/questions/610245。 –
實際上BjörnPollex評論應該是被接受的答案 – Begemoth
「**模板的**依賴名稱」非常具有誤導性,「const_iterator」是** **依賴名稱。 –