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
Read More:
- [Solved] thymeleaf.TemplateEngineException processing template “main“: An error happened during template pars
- Vue Error compiling template: Component template should contain exactly one root element. If you
- [Solved] Vue Error: Failed to mount component: template or render function not defined
- [Solved] org.thymeleaf.exceptions.TemplateInputException: Error resolving template [currentUserCartItems],
- [Solved] Error:couldn‘t connect to server 127.0.0.1:27017, connection attempt failed: SocketException: …
- Template Render Error Solution (Hexo blog theme, GIT upload)
- [Solved] Vue3 Eslint Error: The template root requires exactly one element
- [Solved] C + + compile Error: error: default argument given for parameter 3
- RuntimeError: ONNX export failed: Couldn‘t export operator aten::upsample_bilinear2d
- Template cannot be rendered due to the joint query of populate in mongoose: syntax error: unexpected token r in JSON at position 0
- [Solved] UE4 Error: Couldn‘t find file for package *** requested by async loading code. NameToLoad: ***
- if a proxy or similar is necessary `net.git-fetch-with-cli`, Rust Complete `Couldn‘t resolve host name (Coul
- [Solved] Loadrunner Error: with parameter delimiters is not a parameter.
- [Solved] NVIDIA-SMI has failed because it couldn’t communicate with the NVIDIA drive
- [Solved] Vue.js error: Module build failed: Error: No parser and no file path given, couldn’t infer a parser.
- PHP Parse error: syntax error, unexpected ‘class‘ (T_CLASS), expecting identifier (T_STRING)
- Robo3T Remote Connect MongoDB Error: Failed to refresh ‘Collections‘. Error: ListCollections failed
- Python3: Str.format Keyerror Solution for incoming parameter error
- How to Solve Angular Error: error NG8002: Can‘t bind to ‘ngModel‘ since it isn‘t a known property of ‘input‘.