Problem with compilation of templatized code

Discussion in 'C++' started by josip.krapac@gmail.com, Jun 27, 2008.

  1. Guest

    Hi all,

    I tried to compile the following code:

    <code>

    template<typename T>
    struct foo {
    vector<foo<T> > children;
    };

    template<typename T>
    void g(const foo<T>& a) {
    vector<foo<T> >::const_iterator childIt = a.children.begin(); //
    error: expected `;' before 'childIt'
    }

    </code>

    But my compiler (gcc version 4.1.2 20061115 (prerelease) (Debian
    4.1.1-21)) reports the error (as commented in code).

    I guess that the problem is in templates, because non-templatized code
    compiles fine:

    <code>

    struct foo {
    vector<foo> children;
    };

    void g(const foo& a) {
    vector<foo>::const_iterator childIt = a.children.begin();
    }

    </code>

    Do you know what I'm doing wrong?

    Thank you!

    Best,

    Josip
     
    , Jun 27, 2008
    #1
    1. Advertisements

  2. Kai-Uwe Bux Guest

    wrote:

    > Hi all,
    >
    > I tried to compile the following code:
    >
    > <code>
    >
    > template<typename T>
    > struct foo {
    > vector<foo<T> > children;
    > };
    >
    > template<typename T>
    > void g(const foo<T>& a) {
    > vector<foo<T> >::const_iterator childIt = a.children.begin(); //
    > error: expected `;' before 'childIt'


    const_iterator is a dependent name. You need a hint that it is a typename.

    typename vector< foo<T> >::const_iterator ...

    > }
    >
    > </code>
    >
    > But my compiler (gcc version 4.1.2 20061115 (prerelease) (Debian
    > 4.1.1-21)) reports the error (as commented in code).
    >
    > I guess that the problem is in templates, because non-templatized code
    > compiles fine:
    >
    > <code>
    >
    > struct foo {
    > vector<foo> children;
    > };

    [snip]

    Well, neither version is required to compile. They may, but in that case,
    you have undefined behavior. Inside the class definition of foo, the type
    foo is incomplete. Instantiating the container vector<foo> is then
    undefined behavior according to clause [17.4.3.6/2] of the standard.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jun 27, 2008
    #2
    1. Advertisements

  3. Noah Roberts Guest

    wrote:
    > Hi all,
    >
    > I tried to compile the following code:
    >
    > <code>
    >
    > template<typename T>
    > struct foo {
    > vector<foo<T> > children;
    > };
    >
    > template<typename T>
    > void g(const foo<T>& a) {
    > vector<foo<T> >::const_iterator childIt = a.children.begin(); //
    > error: expected `;' before 'childIt'
    > }


    template < typename T >
    void g(foo<T> const& a)
    {
    typename vector< foo<T> >::const_iterator childIt = a.children.begin();
    }
     
    Noah Roberts, Jun 27, 2008
    #3
    1. Advertisements

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. skscpp

    templatized state model

    skscpp, Sep 8, 2003, in forum: C++
    Replies:
    1
    Views:
    371
    Kasper van den Berg
    Sep 8, 2003
  2. hall
    Replies:
    2
    Views:
    424
  3. Paul Escherton

    Templatized operator () overload

    Paul Escherton, Nov 10, 2003, in forum: C++
    Replies:
    2
    Views:
    474
    tom_usenet
    Nov 10, 2003
  4. Luther Baker

    templatized logging

    Luther Baker, May 21, 2004, in forum: C++
    Replies:
    4
    Views:
    490
    Luther Baker
    May 22, 2004
  5. mrstephengross
    Replies:
    5
    Views:
    843
    Larry I Smith
    May 18, 2005
  6. StephQ
    Replies:
    1
    Views:
    400
  7. kausikd
    Replies:
    0
    Views:
    352
    kausikd
    Aug 6, 2010
  8. Alf P. Steinbach
    Replies:
    1
    Views:
    227
    Alf P. Steinbach
    Dec 18, 2013
Loading...