Problem with compilation of templatized code

J

josip.krapac

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
 
K

Kai-Uwe Bux

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 said:
}

</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
 
N

Noah Roberts

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();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top