470,833 Members | 1,350 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

iterator for template

xu
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
Jun 27 '08 #1
2 1014
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,
--
Vincent Jacques
Jun 27 '08 #2
xu
Thanks! It works.

Jinjun

On May 21, 12:37*pm, Vincent Jacques <newsgr...@vincent-jacques.net>
wrote:
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,
--
Vincent Jacques
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

26 posts views Thread by Michael Klatt | last post: by
reply views Thread by nick | last post: by
3 posts views Thread by chriscorbell | last post: by
reply views Thread by mailforpr | last post: by
1 post views Thread by Axel Gallus | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.