带自定义模板的STL迭代器

时间:2009-02-27 12:17:07

标签: c++ xcode gcc stl

我有以下模板方法,

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

我收到以下编译错误(XCode / gcc)

  

错误:预期`;'在'它'之前

我发现其他人有类似问题here (read down to see it's the same even though it starts out with a different issue),但他们似乎已通过更新Visual Studio解决了问题。这让我觉得它是一个编译器问题而且它应该编译,这是正确的吗?通过索引从0到大小的迭代工作,但它不是我更喜欢实现此功能的方式。还有另一种方法吗? 感谢

3 个答案:

答案 0 :(得分:10)

何时使用typename关键字的经典案例。希望您拥有#include - ed vectoriterator,并在范围内的某处using namespace std;。使用:

typename vector<T>::iterator it;

查找依赖名称。开始here

答案 1 :(得分:1)

我认为你错过了typename

#include <vector>
using namespace std;

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

答案 2 :(得分:0)

尝试:

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

就是这样,它是一个缺少using的陈述?

相关问题