简体   繁体   English

带自定义模板的STL迭代器

[英]STL iterator with custom template

i have the following template method, 我有以下模板方法,

template <class T>
void Class::setData( vector<T> data )
{    
    vector<T>::iterator it;
}

and i'm getting the following compilation error ( XCode/gcc ) 我得到以下编译错误(XCode / gcc)

error: expected `;' 错误:预期`;' before 'it' 在'它'之前

i found someone else with a similar problem here (read down to see it's the same even though it starts out with a different issue) but they seem to have resolved by updating Visual Studio. 我在这里发现了其他有类似问题的人(读下来看它是相同的,即使它是从一个不同的问题开始)但他们似乎已经通过更新Visual Studio解决了。 This makes me guess that it is a compiler issue and that it should compile, is that correct? 这让我觉得它是一个编译器问题而且它应该编译,这是正确的吗? Iteration via indexing from 0 to size works, however it is not the way i would prefer to implement this function. 通过索引从0到大小的迭代工作,但它不是我更喜欢实现此功能的方式。 Is there another way around this? 还有另一种方法吗? Thanks 谢谢

Classic case of when to use the typename keyword. 何时使用typename关键字的经典案例。 Hoping that you have #include -ed vector and iterator and have a using namespace std; 希望你有#include -ed vectoriterator并且有一个using namespace std; somewhere in scope. 在范围的某个地方。 Use: 采用:

typename vector<T>::iterator it;

Look up dependent names. 查找依赖名称。 Start here . 这里开始。

I think you are missing a typename : 我想你错过了一个typename

#include <vector>
using namespace std;

class Class{
public:
    template <class T>
    void setData( vector<T> data ) {
        typename vector<T>::iterator it;
    }
};

Try: 尝试:

template <class T>
void Class::setData( std::vector<T> data )
{    
    std::vector<T>::iterator it;
}

Just is case it's a missing using statement? 就是这种情况,它是一个缺少using声明?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM