Possible Duplicate:
Where and why do I have to put the “template” and “typename” keywords?
map iterator in template function unrecognized by compiler
I have a template function which has std::map::iterator instantiation within it –
template <class B , class C>
C getValue (B& myMap , C returnType) {
map<string,C>::iterator it = myMap.find(var);
// implementation ...
}
and it prompt errors –
In function ‘C getValue(char*, B&, C)’:
error: expected ‘;’ before ‘it’
error: ‘it’ was not declared in this scope
How should I have make it properly ?
It is a dependent type so you need
typename:See Where and why do I have to put the "template" and "typename" keywords? for much more detail.
As commented by Zoidberg’, C++11 has
autowhich instructs the compiler to deduce the type. See Elements of Modern C++ Style for a brief overview (with some other features).