Connecting Tech Pros Worldwide Forums | Help | Site Map

iterator for template

xu@la
Guest
 
Posts: n/a
#1: Jun 27 '08
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

Vincent Jacques
Guest
 
Posts: n/a
#2: Jun 27 '08

re: iterator for template


Hi,

xu@la a écrit :
Quote:
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
xu@la
Guest
 
Posts: n/a
#3: Jun 27 '08

re: iterator for template


Thanks! It works.

Jinjun

On May 21, 12:37*pm, Vincent Jacques <newsgr...@vincent-jacques.net>
wrote:
Quote:
Hi,
>
xu@la a écrit :
>
Quote:
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
Closed Thread