C++ 声明一个C++;集合迭代器

C++ 声明一个C++;集合迭代器,c++,templates,compiler-errors,iterator,C++,Templates,Compiler Errors,Iterator,可能重复: 以下代码将不会编译: #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<in

可能重复:

以下代码将不会编译:

#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;
}
#包括
#包括
使用名称空间std;
样板
无效打印集(集s){
set::迭代器;
}
int main(int argc,字符**argv){
设置s;
打印集;
返回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
set.cpp:在函数“void printSet(std::set)”中:
set.cpp:7:错误:应为“;”在“它”之前
set.cpp:在函数“void printSet(std::set)[with T=int]”中:
set.cpp:12:从此处实例化
set.cpp:7:错误:依赖名称“std::set::iterator”被解析为非类型,但实例化会生成一个类型
cpp:7:注意:如果类型的意思是
我做错了什么?我觉得我几乎什么都没写,而且C++已经给了我这个可怕的信息。
如果有帮助的话,看起来,如果我用迭代器注释掉这行代码,就不会有错误。然而,到目前为止,我在网上看到的所有示例似乎都是这样声明迭代器的。我认为。

在声明前加上关键字
typename
,因此:

typename set<T>::iterator it;

typename
不是必需的。

在声明前加上关键字
typename
,因此:

typename set<T>::iterator it;

typename
不是必需的。

Hmm。。。为什么需要输入名称?谢谢!它起作用了!但这似乎有点像黑魔法。确切地说,我什么时候知道我需要输入一个
typename
?@math4tots:添加了解释。嗯。。。为什么需要输入名称?谢谢!它起作用了!但这似乎有点像黑魔法。确切地说,我什么时候知道我需要输入
typename
?@math4tots:添加了解释。请非常仔细地阅读错误消息的最后两行。请非常仔细地阅读错误消息的最后两行。