# Template argument deduction/substitution failed: couldn‘t deduce template parameter [How to Solve]

Article catalog

Error code error reason solution

Error code

We want to implement a generic function to find the maximum value of the interval. The parameters are left and right iterators, which return the maximum value.

``````#include<iostream>
#include<vector>
using namespace std;
template<class T>
T findmax(typename vector<T>::iterator left,typename vector<T>::iterator right){
T ret = *left;
for (;left!=right;++left){
ret = (*left)>ret ?*left : ret;
}
return ret;
}

int main(){
vector<int> a({3,4,6,2,1,5});
cout << findmax(a.begin(),a.end());
return 0;
}
``````

error:
15:38: error: no matching function for call to ‘findmax(std::vector::iterator, std::vector::iterator)’
15 | cout << findmax(a.begin(),a.end());
5:3: note: candidate: ‘template T findmax(typename std::vector::iterator, typename std::vector::iterator)’
5 | T findmax(typename vector::iterator left,typename vector::iterator right){
5:3: note: template argument deduction/substitution failed:
15:38: note: couldn’t deduce template parameter ‘T’
15 | cout << findmax(a.begin(),a.end());

The reason
The compiler cannot guess that the template T is int based on the type vector< int >::iterator passed in

Solution
Specify the template manually at the place where the function is called.
Replace the used findmax with findmax< int >:

``````#include<iostream>
#include<vector>
using namespace std;
template<class T>
T findmax(typename vector<T>::iterator left,typename vector<T>::iterator right){
T ret = *left;
for (;left!=right;++left){
ret = (*left)>ret ?*left : ret;
}
return ret;
}

int main(){
vector<int> a({3,4,6,2,1,5});
cout << findmax<int>(a.begin(),a.end());
return 0;
}

``````

Run successfully