C++ 模板 std::vector::iterator 错误 [英] C++ template std::vector::iterator error

查看:40
本文介绍了C++ 模板 std::vector::iterator 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C++ 中,我试图为我的模板化类获取一个 std::vector::iterator.但是,当我编译它时,出现错误:error C2146: syntax error : missing ';'在标识符迭代器" 之前,错误 C4430:缺少类型说明符 - 假定为 int.注意:C++ 不支持 default-int.我也收到警告:warning C4346: 'std::vector::iterator' :dependent name is not a type:

In C++, I am trying to get a std::vector::iterator for my templated class. However, when I compile it, I get the errors: error C2146: syntax error : missing ';' before identifier 'iterator', error C4430: missing type specifier - int assumed. Note: C++ does not support default-int. I also get the warning: warning C4346: 'std::vector<T>::iterator' : dependent name is not a type:

#include <vector>
template<class T> class v1{
    typedef std::vector<T>::iterator iterator; // Error here
};
class v2{
    typedef std::vector<int>::iterator iterator; // (This works)
};

我什至尝试过

template<typename T> class v1{
    typedef std::vector<T>::iterator iterator;
};

template<typename T = int> class v1{
    typedef std::vector<T>::iterator iterator;
};

推荐答案

std::vector::iterator 是一个 依赖名称,所以这里需要一个typename来指定它指的是一个类型.否则,假定引用非类型:

std::vector<T>::iterator is a dependent name, so you need a typename here to specify that it refers to a type. Otherwise it is assumed to refer to a non-type:

typedef typename std::vector<T>::iterator iterator;

这篇关于C++ 模板 std::vector::iterator 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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