C ++ <T> :: iterator list cannot be used in derived class template

The g ++ compiler gives this error: expected `; 'before' it

template <typename T>
class myList : public std::list<T>
{   
public:

  void foo () 
  { 
    std::list<T>::iterator it;       // compiler error as above mentioned, why ???
  }
};

Thank.

+5
source share
2 answers

In g ++. whenever in the template you see an error:

error: expected ';' before 'it'

Suppose you need a type name:

typename std::list<T>::iterator it;  

This is necessary if a new type is declared in the template (in this case, a list iterator), which depends on one or more template parameters. The need is not unique to g ++ BTW, it is part of standard C ++.

+14
source

. , typedef , ( ):

template <typename T>
class myList : public std::list<T>
{   
public:
    typedef T value_type;
    typedef const T const_value_type;
    typedef value_type& reference;
    typedef const_value_type& const_reference;
    typedef value_type* pointer;
    typedef const_value_type* const_pointer;

    typedef std::list<T> base_container;
    typedef typename base_container::iterator iterator;
    typedef typename base_container::const_iterator const_iterator;

    void foo () 
    { 
        iterator it; // easy peasy
    }
};

typedef.

, , . , , .

+9

All Articles