简体   繁体   中英

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

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?

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

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

那么typename TList<T>::Type::value_type::iterator呢?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM