• Advertisement
Sign in to follow this  

Gcc compile error, works on VC++

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

Sample code works fine in VC++ 2003, but fails to compile on DevC++. Templates are working fine but i cant seem to use it with stl::map.
template <class T>
class UniqueRegistry
{
	T* getEntry(const String& name)
	{
		std::map<String,T*>::iterator t = passes.find(name);
                 if(t != passes.end())
		{
			return t->second;
		}
		else
		{
			return 0;
		}	
         };
}


In member function `T* UniqueRegistry<T>::getEntry(const String&)': error: expected `;' before "t" error: `t' undeclared (first use this function) Any help?

Share this post


Link to post
Share on other sites
Advertisement
I think you want

template <class T>
class UniqueRegistry
{
T* getEntry(const String& name)
{
typename std::map<String,T*>::iterator t = passes.find(name);
if(t != passes.end())
{
return t->second;
}
else
{
return 0;
}
};
}


But I haven't tested it in a compiler

I added typename.

Share this post


Link to post
Share on other sites
Hmm, is it necessary to add the typename qualifier?

Im not a template guru and hardly use them unless it is really necessary. But lots of tutorials don't seem to suggest using it. Anyway ill give it a try.

Share this post


Link to post
Share on other sites
I think they are required in the C++ standard. Not needing to write them is an extension, which GCC used to support. I guess they removed it to help encourage standards comformance in people's code.

Share this post


Link to post
Share on other sites
It works! Thanks. But i still see some lines where typename is not used and it compiles fine. Anyone knows the exact situation where their use is required and why?

Share this post


Link to post
Share on other sites
I'm not entirely sure where it's needed. (I'm guessing anytime you need to write the <> part of template and are using a typename).

I found
Quote:

the C++ standard rule is that every identifier that involves a template is interpreted as a value unless it is explicitly qualified as a typename.


Here

Share this post


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

  • Advertisement