Posts Tagged ‘Templates’

C++ STL container of templated class objects failing?

Monday, February 16th, 2009

Check this out:

 1: template <class T_>
 2: class SimpleTemplated
 3: {
 4: public:
 5:   T_ i;
 6: };
 7:
 8: template <class T_>
 9: class Container
10: {
11: public:
12:   typedef SimpleTemplated<T_> Composite;
13:   typedef std::list<Composite> Collection;
14:   Collection l;
15:   void run()
16:   {
17:     Collection::iterator iter;
18:   }
19: };

Here’s the pretty colorized version

Line 17 fails compilation (g++, cygwin, vista) saying

scrapyard.cpp: In member function 'void Container<T_>::run()':
scrapyard.cpp:17: error: expected `;' before 'iter'
make: *** [scrapyard.o] Error 1

Anyone? ANYone?

If I change line 12 from:

12:   typedef SimpleTemplated<T_> Composite;

to

12:   typedef std::string Composite;

it works fine of course.

It’s not a direct issue with the stl collection class either. I’ve also tried vector, as well as attempting creation of const_iterator and size_type, all with the same result.

Something about the SimpleTemplated template class is preventing me from declaring a var of a type defined within the containing collection class instantiation.

Pulling my hair out. (This is what happens when I come back to c++ after a year or two off and try something like this first thing. *ugh*)