• Advertisement
Sign in to follow this  

Problem with templates [C++]

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

template <class Vehicle>
class Manager2D
{
public:
	typedef std::map<int,std::multimap<int,Vehicle*>> MapaVehicles;
	typedef std::map<int,std::multimap<int,Vehicle*>>::iterator IteradorMapaVehicles2;
};


so, the compiler is telling me somethink like
"warning C4346: 'std::map<int,std::multimap<int,Vehicle*>>::iterator' : an independent name is not a type
        prefix with 'typename' to indicate a type"

//original (Spanish):

"warning C4346: 'std::map<int,std::multimap<int,Vehicle*>>::iterator' : un nombre dependiente no es un tipo
        prefijo con 'typename' para indicar un tipo"


Where should I put the 'typename' keyword?

Share this post


Link to post
Share on other sites
Advertisement
As the error message is saying, you should prefix the type it's complaining about with typename.
typedef typename std::map<int,std::multimap<int,Vehicle*> >::iterator IteradorMapaVehicles2;

Also note that the closing angle brackets for nested templates need to be separated with a space. If they form a >>, a conformant compiler will parse it as a right shift.
Visual Studio has an extension to support the space-less variant, but it's not standard.

Share this post


Link to post
Share on other sites
I thought I've tried to write anywhere 'typename', it looks I didn't xD. Programming a template is realy painfull (And it seems I'll never know everythink avout how it works). But they really do help once implemented.


Thank you for helping me; rate++ ;-)

Share this post


Link to post
Share on other sites
The reason you need to use typename there is because what iterator is depends on the template parameter Vehicle. To disambiguate and to indicate to the compiler that it is a type, one uses the typename keyword.

Share this post


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

  • Advertisement