I’m re-learning C++, and have started by trying what should be a simple algorithm: QuickSort. My function has this signature:
template <class T>
void QSort(typename std::vector<T>::iterator begin, typename std::vector<T>::iterator end)
And it is called in my main function:
int main()
{
std::vector<int> unsort({56,32,11,45,67,81,12,5});
std::vector<int>::iterator b=unsort.begin();
std::vector<int>::iterator e=unsort.end();
QSort(b, e);
return 0;
}
And gives this error:
C:\Users\Deus\Projects\QSort\main.cpp||In function 'int main()':|
C:\Users\Deus\Projects\QSort\main.cpp|49|error: no matching function for call to 'QSort(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&)'|
||=== Build finished: 1 errors, 0 warnings ===|
It seems that the compiler is having trouble resolving what T should be. Is there a way to do what I’m trying to do, or should I just declare the arguments as type T, and work with the resulting uncertainty?
The compiler has no way to deduce
Tfrom your function call. Think about what happens whenstd::vector<T>::iteratorisT*:In general, if you write
typename Something<TemplateParameter>::anotherThing, then theTemplateParemtercannot be deduced in the call. It must be explicitly providedI recommend to just use
Tas the parameter type. That will allow you to not only accept vector iterators, but alsoT*, orstd::deque<T>::iteratorand any other random access iterators.