Why does the following not compile (MSVC10 – but I suspect it’s not valid C++), and is there a workaround?
template <typename M>
struct MyClass
{
typedef std::vector<M>::iterator iteratorT;
iteratorT myIterator;
};
Error is error C2146: syntax error : missing ';' before identifier 'iteratorT'. I’ve tried a bunch of variations with the same result: you can use std::vector<M>::iterator as a type in a member function, but not as a type of a member variable.
It’s a case of the
typename. Short answer, you need to do this instead:Long answer, the compiler doesn’t know what
std::vector< M >::iteratorresolves to asMcan be anything and there could be a specialization ofstd::vectorfor it. Specifically, it cannot tell ifstd::vector< M >::iteratoris a type or a value, and it believes its a value. You have to explicitly tell the compiler its a type by insertingtypename.