简体   繁体   English

递归 function 中的“没有匹配的 function 用于调用”

[英]"no matching function for call" in recursive function

I have two functions that I am using to attempt to perform a merge sort.我有两个函数用于尝试执行合并排序。 Unfortunately, it claims that my call to merge does not match any existing function definitions- which has me dumbfounded since it appears to match perfectly.不幸的是,它声称我对合并的调用与任何现有的 function 定义都不匹配——这让我目瞪口呆,因为它看起来完全匹配。

// Merge Sort:
vector<int> MergeSort(vector<int> &x) {
  int n = x.size();
  if (n <= 1) {
    return x;
  }
  size_t const half = x.size() / 2;
  vector<int> u(x.begin(), x.begin() + half);
  vector<int> v(x.begin() + half, x.end());
  vector<int> i = MergeSort(u);
  vector<int> j = MergeSort(v);
  vector<int> m = merge(i, j); // no matching function for call to 'merge'
  return m;
}
// Merge vector
vector<int> merge(vector<int> &x, vector<int> &y) {
  vector<int> t;
  while (!x.empty() || !y.empty()) {
    if (x.back() > y.back()) {
      t.push_back(y.back());
      y.pop_back();
    } else {
      t.push_back(x.back());
      x.pop_back();
    }
    if (!x.empty()) {
      t.push_back(x.back());
      x.pop_back();
    } else {
      t.push_back(y.back());
      y.pop_back();
    }
  }
  return t;
}

I have tried a bunch of silly things like changing the order of definitions and even testing this with different variable types but run into the same issue despite my efforts.我尝试了很多愚蠢的事情,比如改变定义的顺序,甚至用不同的变量类型来测试它,但尽管我付出了努力,还是遇到了同样的问题。

I've been messing with this for over an hour now and am completely stuck.我已经搞砸了一个多小时了,现在完全卡住了。 I seriously hope I am not overlooking something obvious.我真的希望我没有忽视一些显而易见的事情。 Any help would be greatly appreciated!任何帮助将不胜感激!

Found the solution.找到了解决方案。 In the post I mentioned that I had already tried switching the function declaration order but had no success.在帖子中我提到我已经尝试切换 function 申报顺序但没有成功。 What I discovered was that by recursively calling MergeSort() in the return line (which is what I had done originally), I got still got the same compiler error, regardless of function declaration order.我发现,通过在返回行中递归调用 MergeSort()(这是我最初所做的),无论 function 声明顺序如何,我仍然遇到相同的编译器错误。 This of course mislead me to believe that the order did not matter since it was a combinative issue.这当然误导我认为顺序无关紧要,因为它是一个组合问题。 Thus, the final solution was changing the function order to be proper and breaking up the single line format.因此,最终的解决方案是将 function 的顺序更改为正确的并打破单行格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM