Johnas
Johnas

Reputation: 1283

C++, iterator to std::map

How to declare an iterator to

std::map <T, Point <T> *> ,

where:

template <typename T>
struct TList
{
    typedef std::vector < std::map <T, Point <T> *> >  Type;
};

In the following code

int main ()
{
    ....
    std::map <T, Point <T> *> ::iterator i_map;  //Error
    ...
}

g++ shows this error:

error: dependent-name `  std::map<T,Point<T>*,std::less<_Key>,std::allocator<std::pair<const T, Point<T>*> > >::iterator' is parsed as a non-type, but instantiation yields a type
note: say `typename  std::map<T,Point<T>*,std::less<_Key>,std::allocator<std::pair<const T, Point<T>*> > >::iterator' if a type is meant

Upvotes: 3

Views: 2409

Answers (4)

Alex
Alex

Reputation: 363

Put "typename" before the line of error : std::map <T, Point <T> *> ::iterator i_map;.

Example:

typename vector<T>::iterator vIdx; 

// On your case : typename std::map <T, Point<T>*>::iterator i_map;

vIdx= find(oVector->begin(), oVector->end(), pElementToFind); //To my case

Upvotes: 0

Alexander Gessler
Alexander Gessler

Reputation: 46657

What about typename TList<T>::Type::value_type::iterator?

Upvotes: 0

Boaz Yaniv
Boaz Yaniv

Reputation: 6424

Does typename std::map <T, Point <T> *> ::iterator i_map; work?

Upvotes: 0

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361762

Use typename as:

  typename std::map<T, Point <T> *>::iterator i_map;
//^^^^^^^^ here!

Because iterator is a dependent-name (as it depends on the map's type argument T), so typename is required here.

Read this FAQ for detail explanation:

Where and why do I have to put the "template" and "typename" keywords?

Upvotes: 5

Related Questions