Correct syntax for map class iterator of class "inner class"?

I want to ask for help with the correct syntax for declaring std :: map, whose mapp_type is the inner class of the template class.

Please find the # if / # else block in the code below. The "#if 1" block has an Outer class that contains an Inner inner class. Outer defines a Func function that accepts a std card whose mapp_type is of type Inner.

#include <map> #if 1 template<typename C, typename T> class Outer { public: Outer(const C& c, const T& t){} virtual ~Outer(){} class Inner { public: Inner(){} Inner(T t){} virtual ~Inner(){} protected: T mT; }; void Func(std::map<C, Inner>& rMap); protected: std::map<C, Inner> mMap; }; template<typename C, typename T> void Outer<C, T>::Func(std::map<C, Outer::Inner>& rMap) { std::map<C, Inner>::iterator iter; for (iter = rMap.begin(); iter != rMap.end(); ++iter) { mMap[iter->first] = iter->second; } } #else class Outer { public: Outer(const int& i, const double& d){} virtual ~Outer(){} class Inner { public: Inner() : mD(0){} Inner(const double d) : mD(d){} virtual ~Inner(){} protected: double mD; }; void Func(std::map<int, Inner>& rMap); protected: std::map<int, Inner> mMap; }; void Outer::Func(std::map<int, Inner>& rMap) { std::map<int, Inner>::iterator iter; for (iter = rMap.begin(); iter != rMap.end(); ++iter) { mMap[iter->first] = iter->second; } } #endif int main() { return 0; } 

Compilation error in Outer :: Func (...) when declaring an iterator std :: map, i.e. this line:

 std::map<C, Inner>::iterator iter; 

I tried, but can't figure out what is wrong with the line of code.

For comparison / contrast, the "#else" block contains a narrow code of a similar nature. This code compiles.

Compilation error and g ++ version:

 >g++ main.cpp main.cpp: In member function 'void Outer<C, T>::Func(std::map<C, Outer<C, T>::Inner, std::less<_Key>, std::allocator<std::pair<const C, Outer<C, T>::Inner> > >&)': main.cpp:31: error: expected ';' before 'iter' main.cpp:33: error: 'iter' was not declared in this scope >g++ --version g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-11) Copyright (C) 2010 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

Thanks for any help.

+7
c ++ dictionary stl inner-classes templates
source share
1 answer

Since Inner is a member of your Owner<C, T> template, it becomes a dependent name. This calls the identifier iterator , which in this case is a member of std::map<C, Inner> , to also become a dependent name.

This forces you to use the typename keyword according to the rules:

 typename std::map<C, Inner>::iterator iter; ~~~^~~~~ 

This is because the compiler cannot be sure what certain constructs inside your class mean, since it does not know the exact types used for C and T :

Inside the template definition (both the template of the template and the template of the function), the value of some constructions may differ from one instance to another. In particular, types and expressions may depend on the types of parameter type parameters and the parameter values ​​of the non-piggy template.

The typename keyword is used to tell the compiler that the character you are referring to is indeed a type of type alias.

+9
source share

All Articles