Typedef矢量模板

typedef vector template

本文关键字:Typedef      更新时间:2023-10-16

我正在尝试在我的课程中添加一些类型,但是编译器在以下代码上报告了语法erron:

    template<class T>
    class MyClass{
        typedef std::vector<T> storageType; //this is fine
        typedef storageType::iterator iterator; //the error is here

,但下一个也无法正常工作:

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

我一直在寻找许多论坛上的答案,但是我找不到解决方案或解决方法。谢谢您的帮助!

您缺少typename

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

有很多类似的问题。例如。查看以下内容:

  • C 模板键入迭代器

std::vector<T>::iterator是一种依赖类型,因此您需要在它之前添加TypeName。

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