Dear fellow programmers,
I fail to get a typedef in a template class to compile, when using G++ 3.4.4 (compiled with 'g++ UnitMyClass.cpp'). But when I use C++ Builder 6's compiler (BCC32) it compiles successfully.
Below are the simplified code and error messages. Note that I already try to tackle the problem with four different syntaxes.
Google didn't give me the answer, I hope you will.
Thanks in advance, Bilderbikkel
[code]
//UnitMyClass.cpp
#include "UnitMyClass.h"
[/code]
[code]
//UnitMyClass.h
#include
Comments
It's because of GCC being strict with the standard and Borland ain't. You gotta write
typedef typename std::map::const_iterator Iter;
However, I don't enough of the C++ standard to explain the details of that.
See ya, Bilderbikkel
The reason is that these typedefs appears in a place where the compiler is uncapable to know what
::const_iterator
is.The compiler must parse, before any instantiation (before any substitution of the template parameter
T
), the templateMyClass
to search syntax errors among other things, but, at that time, sinceT
has no value, the compiler cannot instantiatestd::map<T, double>
, and thus, it has no means to know what is inside it.Keep in mind that each different instantiation of a same template could contain totally diferent things. For example,
map<int, double>::const_iterator
could be atypedef
, butmap<double, double>::const_iterator
could be a static or constant variable, or the name of a function, or an enum constant (we know that it is a type, but the compiler doesn't).So, you have to tell the compiler that what you are expecting is a type, with the keyword
typename
as Lundin said, because otherwise, it treats your sentence as an expression instead of as a declaration.