【问题标题】:typedef vector templatetypedef 向量模板
【发布时间】:2012-10-10 07:08:27
【问题描述】:

我正在尝试向我的类添加一些 typedef,但编译器报告以下代码的语法错误:

    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;

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

【问题讨论】:

    标签: c++ templates vector typedef


    【解决方案1】:

    您缺少typename

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

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

    【讨论】:

      【解决方案2】:

      std::vector&lt;T&gt;::iterator 是依赖类型,所以需要在它前面加上 typename。

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

      【讨论】:

      • 第一个他为什么不需要它?
      • std::vector&lt;T&gt; 不是依赖类型。无需实例化任何模板即可确定其类型。要确定std::vector&lt;T&gt;::iterator 的类型,您必须实例化std::vector&lt;T&gt;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-17
      相关资源
      最近更新 更多