| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"James W. Walker" |
| Date: |
08 Sep 2003 10:56:57 AM |
| Object: |
Question about template error message |
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.
.
|
|
| User: "Stuart Golodetz" |
|
| Title: Re: Question about template error message |
08 Sep 2003 11:14:05 AM |
|
|
"James W. Walker" <osxNOSPAM@jwwalker.com.invalid> wrote in message
news:080920030853086449%osxNOSPAM@jwwalker.com.invalid...
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.
.
|
|
|
| User: "James W. Walker" |
|
| Title: Re: Question about template error message |
08 Sep 2003 11:25:01 AM |
|
|
In article <3f5cab88$0$242$cc9e4d1f@news.dial.pipex.com>, Stuart
Golodetz <sgolodetz@dial.pipex.com> wrote:
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?
.
|
|
|
| User: "tom_usenet" |
|
| Title: Re: Question about template error message |
08 Sep 2003 11:56:03 AM |
|
|
On Mon, 08 Sep 2003 16:25:01 GMT, "James W. Walker"
<osxNOSPAM@jwwalker.com.invalid> wrote:
In article <3f5cab88$0$242$cc9e4d1f@news.dial.pipex.com>, Stuart
Golodetz <sgolodetz@dial.pipex.com> wrote:
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?
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
.
|
|
|
|
|
|

|
Related Articles |
|
|