problem with template

B

bite

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) = &it->second;
i++;
}
}
virtual ~PointerVector () {}
};

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

Noah Roberts

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) = &it->second;
i++;
}
}
virtual ~PointerVector () {}
};

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

Eric Pruneau

"bite" <[email protected]> a écrit dans le message de
(e-mail address removed)...
prefix the above line with "typename".

Wonderful, thank you very much.

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



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


Eric Pruneau
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top