简体   繁体   中英

"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.

// 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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