Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with compilation of templatized code

Reply
Thread Tools

Problem with compilation of templatized code

 
 
josip.krapac@gmail.com
Guest
Posts: n/a
 
      06-27-2008
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
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      06-27-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      06-27-2008
(E-Mail Removed) 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();
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem of overloading operator - templatized kausikd C++ 0 08-06-2010 05:54 AM
Problem with templatized overloaded operator () StephQ C++ 1 03-11-2007 08:24 PM
Invoking templatized base class constructor from templatized derived class constructor mrstephengross C++ 5 05-18-2005 07:12 PM
How do I load data to a templatized class hall C++ 2 10-29-2003 10:30 AM
templatized state model skscpp C++ 1 09-08-2003 08:31 PM



Advertisments