Question about template error message

  • Thread starter James W. Walker
  • Start date
J

James W. Walker

I can't understand why I'm getting an error message from code like
this...

#include <vector>

template <class T>
struct Foo
{
typedef int (T::*Method)( int i );
typedef int (*Func)( int i );

std::vector< Method > mMethodVec;

void Bar()
{
std::vector< Func >::iterator i;
std::vector< Method >::iterator j;
}
};

The error is on the line declaring the iterator j. For some reason the
compiler expects a semicolon before that. There's no error on the mere
declaration of a vector of method pointers, and if I can create the
vector, I ought to be able to have an iterator for it.
 
S

Stuart Golodetz

James W. Walker said:
I can't understand why I'm getting an error message from code like
this...

#include <vector>

template <class T>
struct Foo
{
typedef int (T::*Method)( int i );
typedef int (*Func)( int i );

std::vector< Method > mMethodVec;

void Bar()
{
std::vector< Func >::iterator i;
std::vector< Method >::iterator j;
}
};

The error is on the line declaring the iterator j. For some reason the
compiler expects a semicolon before that. There's no error on the mere
declaration of a vector of method pointers, and if I can create the
vector, I ought to be able to have an iterator for it.

I think it should be:

typename std::vector<Method>::iterator j;

The point being, of course, that it's a dependent name. It's equivalent to
doing:

typename std::vector<int (T::*)(int)>::iterator j;

when the need for typename becomes clearer. The latter fails to compile
under g++ for some reason (even though the corrected version of what you
wrote compiles fine). Both compile without errors under Comeau C++.

HTH,

Stuart.
 
J

James W. Walker

Stuart said:
I think it should be:

typename std::vector<Method>::iterator j;

Thanks! That works.
The point being, of course, that it's a dependent name. It's equivalent to
doing:

typename std::vector<int (T::*)(int)>::iterator j;

when the need for typename becomes clearer.

Umm, I can't say that makes it clearer to me. What would it be other
than a type name, that would cause the compiler to need a hint?
 
T

tom_usenet

Thanks! That works.


Umm, I can't say that makes it clearer to me. What would it be other
than a type name, that would cause the compiler to need a hint?

A static member or member function. If you don't use "typename", the
compiler does assume it is one of those, hence the error. This relates
to the fact that when the code in question is parsed, the compiler
doesn't know about specific specializations of vector, which
theoretically might have a static member variable called iterator.

Tom
 

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
474,141
Messages
2,570,817
Members
47,365
Latest member
BurtonMeec

Latest Threads

Top