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

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

  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. 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. mrstephengross
    Replies:
    5
    Views:
    610
    Larry I Smith
    May 18, 2005
  2. StephQ
    Replies:
    1
    Views:
    328
  3. kausikd
    Replies:
    0
    Views:
    248
    kausikd
    Aug 6, 2010
  4. Alf P. Steinbach
    Replies:
    0
    Views:
    127
    Alf P. Steinbach
    Dec 9, 2013
  5. Tobias Müller
    Replies:
    0
    Views:
    122
    Tobias Müller
    Dec 9, 2013
Loading...

Share This Page