I have a function template to print a map of string with object pointers:
//template function to print map of pointers
template <class T2>
void printMap(std::map<string, T2*>&_map)
{
//test print line
cout << "Test function template for map print" << endl;
std::map<string, T2*>::iterator iter;
for (iter = _map.begin(); iter != _map.end(); iter++)
{
//call print function for type
iter->second->print();
}
}
I am trying to implement it:
printMap<myObject>(std::map<string, myObject*>&myMapOfObjects);
I am getting
"error type name is not allowed"
I am also getting an error on the linux server that a ; is expected before iter and that iter is undeclared.
I have tested the files with a basic function template and the functions are being passed to the file, it's just the way I written this function, it's not happy with what I am doing. I have been working on this for a few days now and am unable to find the solution, I am sure it is obvious to experienced programmers, thanks.
Another question:
can I pass string as a separate type, so have T1 and T2, template<class T1, class T2>
so I can use the template for all sorts of maps, ie with ints?
Your syntax is wrong for calling the function. You can do so simply like this:
And the compiler will deduce the template arguments. Or, if you'd rather be explicit about it:
The
std::map<string, myObject*>&
text does not belong in the invocation.