-1

I'm trying to implement a mergesort tree in C++, but I'm getting the error

note:   template argument deduction/substitution failed:
C:\Users\IT Services\Documents\mergetree.cpp:15:38: note:   candidate expects 6 arguments, 2 provided
   tree[k]=merge(tree[2*k],tree[2*k+1]);

Here's the code:

#include <bits/stdc++.h>

using namespace std;

vector<vector<int>> tree;
int n;

void update (int k, vector<int> vect)
{
    k+=n;
    tree[k]=vect;

    for (k/=2;k>=1;k/=2)
    {
        tree[k]=merge(tree[2*k],tree[2*k+1]);
    }
}

vector<int> merge (vector<int> arr1, vector<int> arr2)
{
    int a1=arr1.size(), a2=arr2.size();
    vector<int> sortvect(a1+a2);

    int i=0,j=0,k=0;

    while (i<a1&&i<a2) sortvect[k++]=((arr1[i]<arr2[j])?arr1[i++]:arr2[j++]);
    while (i<a1) sortvect[k++]=arr1[i++];
    while (j<a2) sortvect[k++]=arr2[j++];

    return sortvect;
}

int main()
{
    cin>>n; tree.resize(2*n,{0});
    int a;

    for (int i=0;i<n;i++)
    {
        cin>>a;
        update(i,{a});
    }
}

Clearly, the merge function does not require 6 arguments. Why does the compiler think it does?

[Also, please ignore the terrible way the code is written. I'm practicing for an upcoming contest, and clean coding conventions are not a priority at the moment.]

user3460322
  • 105
  • 7
  • 3
    Ah, but `std::merge` does. You have fallen victim to the messy combination of `#include ` and `using namespace std;`. Strongly reconsider your reading material if this came recommended. – user4581301 Dec 29 '17 at 03:52
  • 3
    Possible duplicate of [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – user4581301 Dec 29 '17 at 03:54
  • 3
    And also a duplicate of [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – user4581301 Dec 29 '17 at 03:54
  • I also suggest you develop the code using `vector::at()` instead of using `[ ]` to access your elements. This is to ensure that you can check if any of those indices go out-of-bounds of the vectors. By using `at()`, If you go out of bounds, a `std::out_of_range` exception will be thrown instead of undefined behavior or a seg fault. Once it is determined all index usage is ok, change the code back to using `[ ]`. – PaulMcKenzie Dec 29 '17 at 04:05

1 Answers1

1

There is a std::merge API (http://en.cppreference.com/w/cpp/algorithm/merge) in standard algorithm library. And there are flavours of it that takes 6 arguments. Your merge call in line 15 could be giong for one of those functions.

You can make sure of that by checking the candidates that are listed after the error description.

You should forward declare your merge method before the line where you plan to use it. And instead of using namespace std;, resolve the scope explicitly where ever you want to use std methods. On line 15, call merge as ::merge. And if you just want your merge to have file scope put it in an unnamed namespace.

sajas
  • 1,599
  • 1
  • 17
  • 39