可能重複:
Where and why do I have to put the 「template」 and 「typename」 keywords?
c++ template typename iterator聲明一個C++設置迭代
下面的代碼不會編譯:
#include <iostream>
#include <set>
using namespace std;
template<class T>
void printSet(set<T> s){
set<T>::iterator it;
}
int main(int argc, char** argv){
set<int> s;
printSet<int>(s);
return 0;
}
我得到一個錯誤說:
set.cpp: In function ‘void printSet(std::set<T, std::less<_Key>, std::allocator<_CharT> >)’:
set.cpp:7: error: expected `;' before ‘it’
set.cpp: In function ‘void printSet(std::set<T, std::less<_Key>, std::allocator<_CharT> >) [with T = int]’:
set.cpp:12: instantiated from here
set.cpp:7: error: dependent-name ‘std::set<T,std::less<_Key>,std::allocator<_CharT> >::iterator’ is parsed as a non-type, but instantiation yields a type
set.cpp:7: note: say ‘typename std::set<T,std::less<_Key>,std::allocator<_CharT> >::iterator’ if a type is meant
我做錯了什麼?我覺得我幾乎沒有寫任何東西,並且C++已經給了我這個可怕的消息。
如果它有幫助,它看起來像是,如果我用迭代器註釋掉這一行,就沒有錯誤。然而,到目前爲止我在網上看到的所有例子似乎都是這樣聲明迭代器的。我認爲。
非常仔細地閱讀錯誤消息的最後兩行。 –