I'd like to ask help with the correct syntax to declare a std::map whose mapped_type is an inner class of a template class.
Please find in the code below a #if/#else block. The "#if 1" block has template class Outer that contains inner class Inner. The Outer defines function Func that takes a std map whose mapped_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 fails in Outer::Func(...) at the declaration of the std::map iterator, i.e. this line:
std::map<C, Inner>::iterator iter;
I've tried but cannot figure out what is wrong with the line of code.
For comparison/contrast, the "#else" block contains non-template code of similar nature. This code compiles.
The compile error and g++ version are:
>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.
Thank you for any help.