Iterator problem with templates

  • Thread starter Patrick Leslie Polzer
  • Start date
P

Patrick Leslie Polzer

Hi,

I have a problem with iterators at compile-time. The following program
will fail to compile:


#include <vector>

using std::vector;

template <typename T>
struct Test
{
};


template <typename T>
struct TestUser
{
vector<Test<T> > tests;

void test()
{
vector<Test<T> >::const_iterator iter; //line 18
}
};


int main()
{
return(0);
}


because:

tmpltest.cxx:18: error: expected `;' before "iter"


What's that? Is vector<Test<T> >::const_iterator an unknown type?

I hope someone can shed some light on this...
 
P

Peter Kragh

Patrick Leslie Polzer wrote:
template <typename T>
struct TestUser
{
vector<Test<T> > tests;

void test()
{
vector<Test<T> >::const_iterator iter; //line 18
}
};
What's that? Is vector<Test<T> >::const_iterator an unknown type?

No. It's not an unknown type. However, it's a so-called dependant name.
Use typename. E.g.

void test()
{
typename vector<Test<T> >::const_iterator iter; //line 18
}

That shoule do the trick :)

HTH

/Peter
 
P

Patrick Leslie Polzer

Peter said:
No. It's not an unknown type. However, it's a so-called dependant name.
Use typename. E.g.

void test()
{
typename vector<Test<T> >::const_iterator iter; //line 18
}

That shoule do the trick :)
Gah, that hit me a few days ago in another context.
The error message could really be more helpful :(

Many thanks!

Leslie
 

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
474,270
Messages
2,571,353
Members
48,041
Latest member
Oliwen826

Latest Threads

Top