I want to make a function that takes a container T (can be vector, map, list...) as template and a T and a Int as arguments, in this function, we're assuming that T is a container of int, and I want to return the first occurence of the int in the container. Here's the function:
template <class T> int & easyfind(T container, int n)
{
typename T<int>::iterator it;
for (it = container.begin(); it != container.end(); it++)
if (*it == n)
return (*it);
throw (NotFoundException());
}
But the compiler says "expected a qualified name after 'typename'", and when I replace the typename
by class
the compiler says "explicit specialization of non-template class 'T'", how can I get this to work?
T
is a type, not a template. You need
typename T::iterator it;
to access its iterator
type member.
The reason you see code like
std::vector<int>::iterator
is because std::vector
is the name of a template, and you need to specify the template parameter. In your case T
is already an instantiation of a template, so there is no need to specify the parameter.