iterator for template

X

xu@la

Hi,

I have a template map and want to use the iterator. But the compile
failed. Is there any restriction on the (const) iterator for template
type?

Sample code:

template <typename T>
void Foo(const std::map<std::string, std::vector<T> > &data) {
const std::map<std::string, std::vector<T> >::const_iterator it =
data.begin();
// ...
}


Thanks,
xu@la
 
V

Vincent Jacques

Hi,

xu@la a écrit :
template <typename T>
void Foo(const std::map<std::string, std::vector<T> > &data) {
const std::map<std::string, std::vector<T> >::const_iterator it =
data.begin();
// ...
}

const typename std::map<std::string, std::vector<T> >::const_iterator it
^^^^^^^^

The 'problem' is that you are in a template, trying to invoke a
dependent name (your const_iterator depends on T), so you must help the
compiler, and tell it it is a type.

I hope it helps,
 
X

xu@la

Thanks! It works.

Jinjun

Hi,

xu@la a écrit :


const typename std::map<std::string, std::vector<T> >::const_iterator it
       ^^^^^^^^

The 'problem' is that you are in a template, trying to invoke a
dependent name (your const_iterator depends on T), so you must help the
compiler, and tell it it is a type.

I hope it helps,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top