【问题标题】:Cannot pass template argument to std::list<T>::iterator [duplicate]无法将模板参数传递给 std::list<T>::iterator [重复]
【发布时间】:2016-06-29 10:51:59
【问题描述】:

我做了一个容器模板类如下:

template<typename K, typename V>
class hash_table {
 public:
  class iterator {
   private:
    list<V> list_;                 // Works well
    list<V>::iterator it_;         // Fails: Syntax-error "iterator"
    list<int>::iterator it2_;      // Works well
  };
//....
}

谁能告诉我,我在list&lt;V&gt;::iterator it_; 做错了什么?为什么会是语法错误?

【问题讨论】:

标签: c++ templates visual-c++ iterator


【解决方案1】:

正如@songyuanyao 所建议的那样,解决方案是将typename 放在list&lt;V&gt;::iterator 之前,例如:

template<typename K, typename V>
class hash_table {
 public:
  class iterator {
   private:
    list<V> list_;                 // Works well
    typename list<V>::iterator it_;         // No more fails
    list<int>::iterator it2_;      // Works well
  };
//....
}

另请参阅: C++ template typename iterator

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 2013-08-24
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多