0

I'm wondering why I a compile error in the following:

#include <unordered_map>

    template <typename T_Key, typename T_Value>
    class Iterator
    {public:
        //std::unordered_map<T_Key, T_Value>::iterator iter;  // GIVES ERROR syntax error: identifier 'iterator'
        std::unordered_map<int, int>::iterator iter; // WORKS
        
    };
Zebrafish
  • 11,682
  • 3
  • 43
  • 119

1 Answers1

3

Should be

typename std::unordered_map<T_Key, T_Value>::iterator iter;

iterator is what's known as a dependent name, because it's meaning cannot be known without knowing what T_Key and T_Value are. Therefore the compiler needs a hint as to what kind of entity it is. Syntactically it could be a type name or it could be a static class member. typename tells the compiler that it's a type.

I believe C++ rules have changed or are changing on this subject, and typename is required less frequently than before. In contexts where the name can only reasonably be a type, typename will no longer be necessary. Maybe someone else can provide the full details.

john
  • 85,011
  • 4
  • 57
  • 81
  • Doh!!! Again, I've had this problem so many times. I'm on Visual Studio, I believe other compilers give more helpful errors. – Zebrafish Sep 28 '20 at 07:16