The code below gives the error:
error: type ‘std::list<T,std::allocator<_Tp1> >’ is not derived from type ‘Foo<T>’
error: expected ‘;’ before ‘iter’
#include <list>
template <class T> class Foo
{
public:
std::list<T>::iterator iter;
private:
std::list<T> elements;
};
Why and what should this be to be correct?
You need
typename std::list<T>::iterator. This is becauselistdepends on the template parameter, so the compiler cannot know what exactly is the nameiteratorwithin it going to be (well, technically it could know, but the C++ standard doesn’t work that way). The keywordtypenametells the compiler that what follows is a name of a type.