Sign in to follow this  

C++ class template help.

This topic is 3293 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I don't understand why it doesn't work. I can't create the iterator for a particular nested template declaration. Here goes. template<typename T> void GraphNode<T>::doSomething() { list< GraphNode<T>* > data; // works fine data.push_back(new GraphNode<T>()); // works fine too list< GraphNode<T>* >::iterator iter; // Doesn't work and I don't get it! // error message was // error: expected `;' before "iter" } What was it that I do wrongly? Please help. Regards.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
try

typename list< GraphNode<T>* >::iterator iter;


Thanks now it works. This is not a commonly used syntax for declaring iterators, is it?
I rarely see it in examples.
What does it really do, by putting typename in front if the statement?
Are there other usage examples like that as well?

Thanks again.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zerox Millienium
Quote:
Original post by SiCrane
try

typename list< GraphNode<T>* >::iterator iter;


Thanks now it works. This is not a commonly used syntax for declaring iterators, is it?
I rarely see it in examples.
What does it really do, by putting typename in front if the statement?
Are there other usage examples like that as well?

Thanks again.


Only common in templates. The typename keyword tells the compiler that what follows is a type, as opposed to a static member or function or something. Somehow it's hard for the compiler to figure out what's a type and what's not in a template.

Share this post


Link to post
Share on other sites
The typename keyword is used to specify that a qualified identifier of a dependent type is a type. A dependent type is a type the some depends on the type parameters of your template. Since list<GraphNode<T> *> uses T, it's a dependent type. list<GraphNode<T> *>::iterator is then a qualified identifier of a dependent type, so requires the typename keyword. If you had list<GraphNode<int> *>::iterator, it wouldn't require the typename keyword since it no longer depends on T. Similarly, list<GraphNode<T> *> by itself doesn't require the typename keyword since it's not a qualified identifier.

Share this post


Link to post
Share on other sites
Quote:

Somehow it's hard for the compiler to figure out what's a type and what's not in a template.


Or they wanted to avoid the situation where the same code could produce a completely different meaning depending on what types it is instantiated with. Type A has x as a type, type B has x as a static member. Should the template compile for A and B alike, provided that otherwise there's no syntax error. (Although perhaps a lot of issues with dependent and non-dependent names are caused by the implementation difficulties...)

Share this post


Link to post
Share on other sites

This topic is 3293 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this