1

I made a container template class as follow:

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
  };
//....
}

Can someone tell me, what I did wrong at list<V>::iterator it_;? Why should this be an syntax error?

Matthias
  • 908
  • 1
  • 7
  • 22
  • 4
    Try `typename list::iterator it_;`. – songyuanyao Jun 29 '16 at 10:53
  • 4
    See [Where and why do I have to put the “template” and “typename” keywords?](http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – songyuanyao Jun 29 '16 at 10:54

1 Answers1

1

As @songyuanyao sugested, the solution is to put typename before the list<V>::iterator like in:

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
  };
//....
}

See also: C++ template typename iterator

Community
  • 1
  • 1
mtb
  • 1,350
  • 16
  • 32