Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

problem with template

0 views
Skip to first unread message

bite

unread,
Sep 29, 2009, 12:04:22 PM9/29/09
to
Anybody could tell me what is wrong with these few lines of code?

#include <map>
#include <vector>

template<class Key, class Value>
#include <map>
#include <vector>

template<class Key, class Value>
class PointerVector : public std::vector<Value *>
{
public:
PointerVector () {}
PointerVector (std::map<Key, Value> & as) : std::vector<Value *>
(as.size ())
{
int i = 0;
std::map<Key, Value>::iterator it;
for (it = as.begin ();
it != as.end ();
it++) {
(*this)[i] = &it->second;
i++;
}
}
virtual ~PointerVector () {}
};

I get " error: expected `;' before ‘it’" at line 12 (declaring the
iterator) when compiling with g++ 4.2.4

Noah Roberts

unread,
Sep 29, 2009, 12:45:20 PM9/29/09
to
In article <c4e7b0bc-472b-4e44-989a-6e19b328bed6
@m38g2000yqd.googlegroups.com>, anonimo....@gmail.com says...

>
> Anybody could tell me what is wrong with these few lines of code?
>
> #include <map>
> #include <vector>
>
> template<class Key, class Value>
> #include <map>
> #include <vector>
>
> template<class Key, class Value>
> class PointerVector : public std::vector<Value *>
> {
> public:
> PointerVector () {}
> PointerVector (std::map<Key, Value> & as) : std::vector<Value *>
> (as.size ())
> {
> int i = 0;
> std::map<Key, Value>::iterator it;

prefix the above line with "typename".

> for (it = as.begin ();
> it != as.end ();
> it++) {
> (*this)[i] = &it->second;
> i++;
> }
> }
> virtual ~PointerVector () {}
> };
>

> I get " error: expected `;' before ?it?" at line 12 (declaring the

bite

unread,
Sep 29, 2009, 12:50:20 PM9/29/09
to
On 29 Set, 18:45, Noah Roberts <d...@reply.com> wrote:
> In article <c4e7b0bc-472b-4e44-989a-6e19b328bed6
> @m38g2000yqd.googlegroups.com>, anonimo.passa...@gmail.com says...

>
>
>
>
>
> > Anybody could tell me what is wrong with these few lines of code?
>
> > #include <map>
> > #include <vector>
>
> > template<class Key, class Value>
> > #include <map>
> > #include <vector>
>
> > template<class Key, class Value>
> > class PointerVector : public std::vector<Value *>
> > {
> > public:
> >   PointerVector () {}
> >   PointerVector (std::map<Key, Value> & as) : std::vector<Value *>
> > (as.size ())
> >   {
> >            int i = 0;
> >            std::map<Key, Value>::iterator it;
>
> prefix the above line with "typename".
>

Wonderful, thank you very much.

Now I'll try to learn what 'typename' is :)

Eric Pruneau

unread,
Sep 29, 2009, 10:07:15 PM9/29/09
to

"bite" <anonimo....@gmail.com> a �crit dans le message de news:
a6f1c66d-5a5f-4670...@d10g2000yqh.googlegroups.com...

It is to clarify that an identifier inside a template is a type.


Eric Pruneau


0 new messages