Template problem with g++

Discussion in 'C++' started by Fab, Mar 2, 2007.

  1. Fab

    Fab Guest

    All,
    I need your help understanding why the following code does *NOT*
    compile with G++ (tested with gcc 3.x and 4.1.x):
    ---------------------------------------------------------------------
    template<class T>
    class myvector { // Just a simplification of std::vector
    class iterator {
    T a;
    iterator &operator++();
    };

    iterator &begin();
    iterator &end();
    };

    template<class T>
    struct mytype {
    T a;
    T b;
    };

    template<class T>
    class Foo {

    int abc() {
    myvector<mytype<T> > foo;
    // DOES work:
    //for (myvector<mytype<int> >::iterator iter = foo.begin();
    iter != foo.end(); ++iter) {

    // DOES NOT work:
    for (myvector<mytype<T> >::iterator iter = foo.begin(); iter !
    = foo.end(); ++iter) {
    // ...
    }
    return 0;
    }
    };
    ---------------------------------------------------------------------

    In method Foo::abc if I specify int as template argument for mytype,
    everything works, but if I want to use T (the template argument of Foo
    class), the compiler reports this error:

    foo.cpp: In member function 'int Foo<T>::abc()':
    foo.cpp:28: error: expected `;' before 'iter'
    foo.cpp:28: error: 'iter' was not declared in this scope

    I haven't personally testes, but apparently the Microsoft compiler
    (Visual Studio 2005) accept that code.

    Any help is appreciated!
    Thanks,
    Fabrizio
    Fab, Mar 2, 2007
    #1
    1. Advertising

  2. Fab

    Mark P Guest

    Fab wrote:
    > All,
    > I need your help understanding why the following code does *NOT*
    > compile with G++ (tested with gcc 3.x and 4.1.x):
    > ---------------------------------------------------------------------
    > template<class T>
    > class myvector { // Just a simplification of std::vector
    > class iterator {
    > T a;
    > iterator &operator++();
    > };
    >
    > iterator &begin();
    > iterator &end();
    > };
    >
    > template<class T>
    > struct mytype {
    > T a;
    > T b;
    > };
    >
    > template<class T>
    > class Foo {
    >
    > int abc() {
    > myvector<mytype<T> > foo;
    > // DOES work:
    > //for (myvector<mytype<int> >::iterator iter = foo.begin();
    > iter != foo.end(); ++iter) {
    >
    > // DOES NOT work:
    > for (myvector<mytype<T> >::iterator iter = foo.begin(); iter !
    > = foo.end(); ++iter) {


    for( typename myvector<mytype<T> >::iterator...

    > // ...
    > }
    > return 0;
    > }
    > };
    Mark P, Mar 2, 2007
    #2
    1. Advertising

  3. Fab

    Fab Guest


    > for( typename myvector<mytype<T> >::iterator...


    A-AH! Gotcha!
    Thanks Mark!

    Fab
    Fab, Mar 2, 2007
    #3
  4. Fab

    Piyo Guest

    Fab wrote:
    > All,
    > I need your help understanding why the following code does *NOT*
    > compile with G++ (tested with gcc 3.x and 4.1.x):
    > ---------------------------------------------------------------------
    > template<class T>
    > class myvector { // Just a simplification of std::vector
    > class iterator {
    > T a;
    > iterator &operator++();
    > };
    >
    > iterator &begin();
    > iterator &end();
    > };
    >
    > template<class T>
    > struct mytype {
    > T a;
    > T b;
    > };
    >
    > template<class T>
    > class Foo {
    >
    > int abc() {
    > myvector<mytype<T> > foo;
    > // DOES work:
    > //for (myvector<mytype<int> >::iterator iter = foo.begin();
    > iter != foo.end(); ++iter) {
    >
    > // DOES NOT work:
    > for (myvector<mytype<T> >::iterator iter = foo.begin(); iter !
    > = foo.end(); ++iter) {
    > // ...
    > }
    > return 0;
    > }
    > };
    > ---------------------------------------------------------------------
    >
    > In method Foo::abc if I specify int as template argument for mytype,
    > everything works, but if I want to use T (the template argument of Foo
    > class), the compiler reports this error:
    >
    > foo.cpp: In member function 'int Foo<T>::abc()':
    > foo.cpp:28: error: expected `;' before 'iter'
    > foo.cpp:28: error: 'iter' was not declared in this scope
    >
    > I haven't personally testes, but apparently the Microsoft compiler
    > (Visual Studio 2005) accept that code.
    >
    > Any help is appreciated!
    > Thanks,
    > Fabrizio
    >


    Whew! Now this will work. The main problem was the typename
    keyword was required. To get the program to be errorless was
    a another story :)

    HTH

    -----------------------------------------------------------
    template<class T>
    class myvector
    { // Just a simplification of std::vector
    public:
    class iterator
    {
    public:
    T a;
    bool operator!=( const iterator &b ) { return true; }
    iterator & operator++() { return *this; }
    iterator operator++( int ) { return *this; }
    };

    iterator begin() { return iterator(); }
    iterator end() { return iterator(); }
    };

    template<class T>
    struct mytype {
    T a;
    T b;
    };

    template<class T>
    class Foo
    {
    public:
    void abc()
    {
    myvector<mytype<T> > foo;
    // DOES work:
    //for (myvector<mytype<int> >::iterator iter = foo.begin();
    //iter != foo.end(); ++iter) {

    // DOES NOT work:
    for (typename myvector<mytype<T> >::iterator iter = foo.begin();
    iter != foo.end(); ++iter)
    {

    }

    }
    };

    int
    main()
    {
    Foo<int> test;
    test.abc();
    return 0;
    }
    Piyo, Mar 2, 2007
    #4
  5. Fab

    Fab Guest

    Piyo,
    Thanks for your reply as well. The program I sent was just to describe
    my problem.
    In my application at the end I use std::vector, but in my test I
    wanted to rule out any possible problem related to how STL are
    implemented in my distribution (in particular, I wanted to focus on
    compiler behavior).

    I can't say that today I didn't learn anything...

    Thanks again!
    Fab


    On Mar 1, 4:18 pm, Piyo <> wrote:
    > Fab wrote:
    > > All,
    > > I need your help understanding why the following code does *NOT*
    > > compile with G++ (tested with gcc 3.x and 4.1.x):
    > > ---------------------------------------------------------------------
    > > template<class T>
    > > class myvector { // Just a simplification of std::vector
    > > class iterator {
    > > T a;
    > > iterator &operator++();
    > > };

    >
    > > iterator &begin();
    > > iterator &end();
    > > };

    >
    > > template<class T>
    > > struct mytype {
    > > T a;
    > > T b;
    > > };

    >
    > > template<class T>
    > > class Foo {

    >
    > > int abc() {
    > > myvector<mytype<T> > foo;
    > > // DOES work:
    > > //for (myvector<mytype<int> >::iterator iter = foo.begin();
    > > iter != foo.end(); ++iter) {

    >
    > > // DOES NOT work:
    > > for (myvector<mytype<T> >::iterator iter = foo.begin(); iter !
    > > = foo.end(); ++iter) {
    > > // ...
    > > }
    > > return 0;
    > > }
    > > };
    > > ---------------------------------------------------------------------

    >
    > > In method Foo::abc if I specify int as template argument for mytype,
    > > everything works, but if I want to use T (the template argument of Foo
    > > class), the compiler reports this error:

    >
    > > foo.cpp: In member function 'int Foo<T>::abc()':
    > > foo.cpp:28: error: expected `;' before 'iter'
    > > foo.cpp:28: error: 'iter' was not declared in this scope

    >
    > > I haven't personally testes, but apparently the Microsoft compiler
    > > (Visual Studio 2005) accept that code.

    >
    > > Any help is appreciated!
    > > Thanks,
    > > Fabrizio

    >
    > Whew! Now this will work. The main problem was the typename
    > keyword was required. To get the program to be errorless was
    > a another story :)
    >
    > HTH
    >
    > -----------------------------------------------------------
    > template<class T>
    > class myvector
    > { // Just a simplification of std::vector
    > public:
    > class iterator
    > {
    > public:
    > T a;
    > bool operator!=( const iterator &b ) { return true; }
    > iterator & operator++() { return *this; }
    > iterator operator++( int ) { return *this; }
    > };
    >
    > iterator begin() { return iterator(); }
    > iterator end() { return iterator(); }
    >
    > };
    >
    > template<class T>
    > struct mytype {
    > T a;
    > T b;
    >
    > };
    >
    > template<class T>
    > class Foo
    > {
    > public:
    > void abc()
    > {
    > myvector<mytype<T> > foo;
    > // DOES work:
    > //for (myvector<mytype<int> >::iterator iter = foo.begin();
    > //iter != foo.end(); ++iter) {
    >
    > // DOES NOT work:
    > for (typename myvector<mytype<T> >::iterator iter = foo.begin();
    > iter != foo.end(); ++iter)
    > {
    >
    > }
    >
    > }
    >
    > };
    >
    > int
    > main()
    > {
    > Foo<int> test;
    > test.abc();
    > return 0;
    >
    > }
    Fab, Mar 2, 2007
    #5
  6. Fab

    Michael Guest

    > for( typename myvector<mytype<T> >::iterator...

    This occurs so frequently that I propose the 80/20 rule:
    80% of all template problems posted to comp.lang.c++ are due to a
    missing 'typename' keyword. 20% are other problems.

    Michael
    Michael, Mar 2, 2007
    #6
  7. Fab

    Alan Johnson Guest

    Michael wrote:
    >> for( typename myvector<mytype<T> >::iterator...

    >
    > This occurs so frequently that I propose the 80/20 rule:
    > 80% of all template problems posted to comp.lang.c++ are due to a
    > missing 'typename' keyword. 20% are other problems.
    >
    > Michael
    >


    Somewhat surprisingly, we don't have anything about this in the FAQ
    (that I can find). 35.18 sort of covers it, but not in a way that
    someone looking for the answer to this variation of the problem would be
    likely to understand.
    Alan Johnson, Mar 2, 2007
    #7
  8. Fab

    Mark P Guest

    Alan Johnson wrote:
    > Michael wrote:
    >>> for( typename myvector<mytype<T> >::iterator...

    >>
    >> This occurs so frequently that I propose the 80/20 rule:
    >> 80% of all template problems posted to comp.lang.c++ are due to a
    >> missing 'typename' keyword. 20% are other problems.
    >>
    >> Michael
    >>

    >
    > Somewhat surprisingly, we don't have anything about this in the FAQ
    > (that I can find). 35.18 sort of covers it, but not in a way that
    > someone looking for the answer to this variation of the problem would be
    > likely to understand.


    Indeed, when I wrote my reply, I began, "This is an FAQ," and went off
    to search for the relevant link item. Then I discovered the same thing
    you did. (Now I didn't try very hard with the search feature so maybe
    it's categorized under one of the miscellaneous chapters.)
    Mark P, Mar 2, 2007
    #8
  9. Fab

    Marcus Kwok Guest

    Mark P <> wrote:
    > Alan Johnson wrote:
    >> Michael wrote:
    >>> This occurs so frequently that I propose the 80/20 rule:
    >>> 80% of all template problems posted to comp.lang.c++ are due to a
    >>> missing 'typename' keyword. 20% are other problems.

    >>
    >> Somewhat surprisingly, we don't have anything about this in the FAQ
    >> (that I can find). 35.18 sort of covers it, but not in a way that
    >> someone looking for the answer to this variation of the problem would be
    >> likely to understand.

    >
    > Indeed, when I wrote my reply, I began, "This is an FAQ," and went off
    > to search for the relevant link item. Then I discovered the same thing
    > you did. (Now I didn't try very hard with the search feature so maybe
    > it's categorized under one of the miscellaneous chapters.)


    I agree. I just sent an email to Marshall Cline suggesting that it be
    added, and pointed him to this thread.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Mar 2, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Theis
    Replies:
    2
    Views:
    467
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    534
    tom_usenet
    Jul 24, 2003
  3. Replies:
    1
    Views:
    2,104
    Gianni Mariani
    Jun 8, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    771
    Thomas J. Gritzan
    Oct 26, 2008
  5. nguillot
    Replies:
    5
    Views:
    530
Loading...

Share This Page