Question about template error message

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James W. Walker

    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.
  • Stuart Golodetz

    #2
    Re: Question about template error message

    "James W. Walker" <osxNOSPAM@jwwa lker.com.invali d> wrote in message
    news:0809200308 53086449%osxNOS PAM@jwwalker.co m.invalid...[color=blue]
    > 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.[/color]

    I think it should be:

    typename std::vector<Met hod>::iterator j;

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

    typename std::vector<int (T::*)(int)>::i terator 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.


    Comment

    • James W. Walker

      #3
      Re: Question about template error message

      In article <3f5cab88$0$242 $cc9e4d1f@news. dial.pipex.com> , Stuart
      Golodetz <sgolodetz@dial .pipex.com> wrote:
      [color=blue]
      > I think it should be:
      >
      > typename std::vector<Met hod>::iterator j;[/color]

      Thanks! That works.
      [color=blue]
      > The point being, of course, that it's a dependent name. It's equivalent to
      > doing:
      >
      > typename std::vector<int (T::*)(int)>::i terator j;
      >
      > when the need for typename becomes clearer.[/color]

      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?

      Comment

      • tom_usenet

        #4
        Re: Question about template error message

        On Mon, 08 Sep 2003 16:25:01 GMT, "James W. Walker"
        <osxNOSPAM@jwwa lker.com.invali d> wrote:
        [color=blue]
        >In article <3f5cab88$0$242 $cc9e4d1f@news. dial.pipex.com> , Stuart
        >Golodetz <sgolodetz@dial .pipex.com> wrote:
        >[color=green]
        >> I think it should be:
        >>
        >> typename std::vector<Met hod>::iterator j;[/color]
        >
        >Thanks! That works.
        >[color=green]
        >> The point being, of course, that it's a dependent name. It's equivalent to
        >> doing:
        >>
        >> typename std::vector<int (T::*)(int)>::i terator j;
        >>
        >> when the need for typename becomes clearer.[/color]
        >
        >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?[/color]

        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

        Comment

        Working...