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.
c ++ dictionary stl inner-classes templates
Stonethrow
source share