声明一个C ++集合迭代器 [英] declaring a C++ set iterator

查看:226
本文介绍了声明一个C ++集合迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:



c ++模板typename迭代器


以下代码将无法编译:

  #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:在函数'void printSet(std :: set< T,std :: less< _Key>,std :: allocator< _CharT> :
set.cpp:7:error:expected`;'before'it'
set.cpp:在函数'void printSet(std :: set< T,std :: less< _Key& std :: allocator< _CharT>>)[with T = int]':
set.cpp:12:从这里实例化
set.cpp:7:error:dependent-name'std :: set< T,std :: less< _Key>,std :: allocator< _CharT> > :: iterator'被解析为非类型,但实例化产生类型
set.cpp:7:note:say'typename std :: set< T,std :: less< _Key>,std :: allocator< _CharT> > :: iterator'如果类型意味着

我做错了什么?我觉得我几乎没有写任何东西,并且已经C ++给我这个可怕的消息。



如果它是有帮助的,看起来像,如果我注释掉行用迭代器,没有错误。但是,到目前为止,我在线上看到的所有示例都是这样声明迭代器。我认为。

解决方案

在关键字 typename 前声明:

  typename set< T> :: iterator it; 

每当引用依赖于模板参数的类型时,您需要这样做。在这种情况下, iterator 取决于T.否则,编译器很难设法找出迭代器无论是类型,还是静态成员或其他任何东西。



请注意,如果你有一个混凝土类型,例如:

  set< int> :: iterator it; 

不需要 typename p>

Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?
c++ template typename iterator

The following code will not compile:

#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;
}

I get an error saying:

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

What did I do wrong? I feel like I've hardly written anything, and already C++ gives me this scary message.

In case it is helpful, it looks like, if I comment out the line with the iterator, there are no errors. However, all the examples I've seen online so far seem to declare iterators this way. I think.

解决方案

Precede your declaration with the keyword typename, thusly:

typename set<T>::iterator it;

You need to do this whenever you refer to a type that is dependent upon a template argument. In this case, iterator is dependent on T. Otherwise, the compiler has difficulty trying to figure out what iterator is, whether it's a type, or a static member or something else. (well, technically, it doesn't try to figure it out, its decision is already made, it just happens to be wrong)

Note that if you have a concrete type, for example:

set<int>::iterator it;

The typename is not necessary.

这篇关于声明一个C ++集合迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆