PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Problem with compilation of templatized code
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Problem with compilation of templatized code

Réponse
 
LinkBack Outils de la discussion
Vieux 27/06/2008, 16h59   #1
josip.krapac@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Problem with compilation of templatized code

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
  Réponse avec citation
Vieux 27/06/2008, 17h19   #2
Kai-Uwe Bux
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with compilation of templatized code

josip.krapac@gmail.com 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
  Réponse avec citation
Vieux 27/06/2008, 17h20   #3
Noah Roberts
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Problem with compilation of templatized code

josip.krapac@gmail.com 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();
}
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h31.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,08993 seconds with 11 queries