本文介绍了C ++为什么错误“无匹配函数”出现时它100%看起来像他们匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我不明白为什么我得到一个错误,说明我的函数不匹配我定义的模板函数。对我来说,他们看起来 是一样的。这是调试中的错误: 错误:没有匹配的函数调用'mergesort'newVec = mergesort(vec.begin因此,我可以学习和编写更好的通用函数和模板,我需要做些什么来改变错误消失了? (只是为了清楚,我不是要求帮助我的mergesort算法 - 我知道它有问题,但我会工作他们。) #include< iostream> #include< vector> using namespace std; template< typename T> vector< T> mergeesort(typename vector< T> :: iterator,typename vector< T> :: iterator); int main(){ vector< int> vec {50,5,40,10,30,15,20,20,10,25}; vector< int> newVec; newVec = mergesort(vec.begin(),vec.end()); //不匹配??? cout<< 之前:; for(auto x:vec)cout<< x<< ''; cout '; cout<< after:; for(auto x:newVec)cout<< x<< ''; cout '; return 0; } template< typename T> vector< T> mergeesort(typename vector< T> :: iterator begin,typename vector< T> :: iterator end){ vector& newVector; copy(begin,end,newVector); if(begin!= end){ vector< T> tmp1; vector< T> tmp2; auto dist = distance(newVector.begin(),newVector.end()); auto distance1 = dist / 2; auto distance2 =(dist / 2 + 1); auto mid1 = newVector.begin(); auto mid2 = newVector.begin(); advance(mid1,distance1); advance(mid2,distance2); tmp1 = mergesort(newVector.begin(),mid1); tmp2 = mergesort(mid2,newVector.end()); merge(tmp1.begin(),mid1,mid2,tmp2.end(),newVector.begin()); return newVector; } else { return newVector; } } 解决方案问题是这是一个非推导的上下文: template< typename T> 向量< T> mergeesort(typename vector< T> :: iterator,typename vector< T> :: iterator); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 有两个 好的答案为什么此函数调用无法编译。 至于如何解决它 - 即使上面的代码工作,没有理由限制您的功能到向量迭代器。您也可以合并排序其他容器。所以只要推导出任何迭代器: template< typename Iterator> vector< T> mergesort(迭代器,迭代器); 此外,通常我们通常希望合并修改提供的范围,容器。所以真的更喜欢: 模板< typename迭代器> void mergesort(Iterator,Iterator); 您的代码还有其他一些问题。调用 std :: merge()此处: merge tmp1.begin(),mid1,mid2,tmp2.end(),newVector.begin()); newVector 我们只是要覆盖它不拥有的内存。您将需要: newVector.reserve(dist); merge(tmp1.begin(),mid1,mid2,tmp2.end(),std :: back_inserter(newVector)); I don't understand why I'm getting an error that states my function doesn't match my defined template function. To me, they look exactly the same. Here is the error in my debug:error: no matching function for call to 'mergesort' newVec = mergesort(vec.begin(),vec.end()); So I can learn and write better generic functions and templates, what do I need to change to make that error go away? (Just to be clear, I'm not asking for help with my mergesort algorithm - I know it has problems, but I'll work them out.)#include <iostream>#include <vector>using namespace std;template <typename T>vector<T> mergesort(typename vector<T>::iterator, typename vector<T>::iterator);int main() { vector<int> vec{ 50, 5, 40, 10, 30, 15, 20, 20, 10, 25 }; vector<int> newVec; newVec = mergesort(vec.begin(),vec.end()); //Doesn't match??? cout << "before:"; for (auto x : vec) cout << x << ' '; cout << '\n'; cout << "after :"; for (auto x : newVec) cout << x << ' '; cout << '\n'; return 0;}template <typename T>vector<T> mergesort(typename vector<T>::iterator begin, typename vector<T>::iterator end){ vector<T> newVector; copy(begin, end, newVector); if(begin!=end){ vector<T> tmp1; vector<T> tmp2; auto dist = distance(newVector.begin(),newVector.end()); auto distance1 = dist/2; auto distance2 = (dist/2+1); auto mid1 = newVector.begin(); auto mid2 = newVector.begin(); advance(mid1,distance1); advance(mid2,distance2); tmp1 = mergesort(newVector.begin(), mid1); tmp2 = mergesort(mid2, newVector.end()); merge(tmp1.begin(), mid1, mid2, tmp2.end(), newVector.begin()); return newVector; } else { return newVector; }} 解决方案 The problem is that this is a non-deduced context:template <typename T>vector<T> mergesort(typename vector<T>::iterator, typename vector<T>::iterator); ^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^There are two good answers for why this function call fails to compile.As to how to fix it - even if the above code did work, there's no reason to limit your functionality to vector iterators anyway. You can merge-sort other containers too. So just deduce any iterator:template <typename Iterator>vector<T> mergesort(Iterator, Iterator);Moreover, typically we'd typically expect the merge to be modifying the range provided, not returning a new container. So really prefer:template <typename Iterator>void mergesort(Iterator, Iterator);There's a few other issues in your code. The call to std::merge() here:merge(tmp1.begin(), mid1, mid2, tmp2.end(), newVector.begin());newVector is empty before this call, so we're just going to overwrite memory that it doesn't own. You'll want to do:newVector.reserve(dist);merge(tmp1.begin(), mid1, mid2, tmp2.end(), std::back_inserter(newVector)); 这篇关于C ++为什么错误“无匹配函数”出现时它100%看起来像他们匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-23 14:33