Jump to content
  • Advertisement
Sign in to follow this  
thedevdan

*Insane* template problem

This topic is 5474 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

This doesn't compile (VC2003.NET):
#include <list>

template <class Type>
class SomeClass
{
	std::list<Type> someList;
	std::list<Type>::iterator someIterator;
};
int main()
{
	return 0;
}
The errors:
Main.cpp(7) : warning C4346: 'std::list<_Ty>::iterator' : dependent name is not a type
        prefix with 'typename' to indicate a type
Main.cpp(8) : see reference to class template instantiation 'SomeClass<Type>' being compiled
Main.cpp(7) : error C2146: syntax error : missing ';' before identifier 'someIterator'
Main.cpp(7) : error C2501: 'SomeClass<Type>::someIterator' : missing storage-class or type specifiers
Iterator - 2 error(s), 1 warning(s)

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by thedevdan
This doesn't compile (VC2003.NET):

*** Source Snippet Removed ***

The errors:

Main.cpp(7) : warning C4346: 'std::list<_Ty>::iterator' : dependent name is not a type
prefix with 'typename' to indicate a type
Main.cpp(8) : see reference to class template instantiation 'SomeClass<Type>' being compiled
Main.cpp(7) : error C2146: syntax error : missing ';' before identifier 'someIterator'
Main.cpp(7) : error C2501: 'SomeClass<Type>::someIterator' : missing storage-class or type specifiers
Iterator - 2 error(s), 1 warning(s)


You need to tell the compiler that std::list<Type>::iterator is a type (it could technically be a datamember from the context of the template). To do this you use the typename keyword:


typename std::list<Type>::iterator someIterator;




Edit:how come typename isn't colored blue in C++ code tags. *Tisk* *Tisk*

Share this post


Link to post
Share on other sites
Wow...

Thanks a lot!

I guess because of PTS the compiler can't tell it iterator is a type or a member. I didn't think of that. Thanks again!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!