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 because list
depends on the template parameter, so the compiler cannot know what exactly is the name iterator
within it going to be (well, technically it could know, but the C++ standard doesn't work that way). The keyword typename
tells the compiler that what follows is a name of a type.
You need a typename
template <class T> class Foo {
public:
typename std::list<T>::iterator iter;
private:
std::list<T> elements;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With