【问题标题】:C++ - iterator for a vector of iterators not compilingC ++ - 迭代器向量的迭代器未编译
【发布时间】:2014-04-07 17:01:47
【问题描述】:

我有一个模板类 P,它有一个 const_iterator,我正在尝试制作一个所述迭代器的向量并遍历该向量:

std::vector<typename P<A, B>::const_iterator>::const_iterator it;

问题是当我尝试编译时我得到了

error: expected ‘;’ before ‘it’

关于为什么会发生这种情况的任何想法?

【问题讨论】:

  • 你能把const_iterator的typedef粘贴到P里吗?
  • 我不能 =( 但我保证它工作正常。

标签: c++ templates iterator stdvector


【解决方案1】:

std::vector&lt;&gt; 之前还需要一个typename,因为P&lt;A, B&gt; 中的至少一个模板参数是依赖类型:

typename std::vector<typename P<A, B>::const_iterator>::const_iterator it;

【讨论】:

    【解决方案2】:

    const_iterator 的两种用法都依赖于模板参数;所以两者都需要typename

    typename std::vector<typename P<A, B>::const_iterator>::const_iterator it;
    ^^^^^^^^             ^^^^^^^^
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 2015-11-19
      • 2013-02-07
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多