|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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(); } |
|
![]() |
| Outils de la discussion | |
|
|