Leoid9
Leoid9

Reputation: 91

C++ Error - no matching function for call

I am having some problems trying to compile my code because of an error.

It is in function mergeSort , line: merge(a, from, mid, to); :

Invalid arguments ' Candidates are: 2 merge(#0, #0, #1, #1, #2, #3) 2 merge(#0, #0, #1, #1, #2) ' no matching function for call to merge(std::vector >&, int&, int&, int&)

    void mergeSort(vector<string> &a, int from, int to)
    {
        if (from == to)
        {
            return;
        }
        int mid = (from + to) / 2;

        mergeSort(a, from, mid);
        mergeSort(a, mid + 1, to);
        merge(a, from, mid, to);

    } // end mergeSort

     void merge(vector<string> &a, int from, int mid, int to)
    {
        int n = to - from + 1; 
        vector<string> b(n); 
        int i1 = from;
        int i2 = mid + 1; 
        int j = 0; 

        while (i1 <= mid && i2 <= to)
        {
            if (a[i1].compare(a[i2]) < 0)
            {
                b[j] = a[i1];
                i1++;
            }
            else
            {
                b[j] = a[i2];
                i2++;
            }
            j++;
        }

        while (i1 <= mid)
        {
            b[j] = a[i1];
            i1++;
            j++;
        }

        while (i2 <= to)
        {
            b[j] = a[i2];
            i2++;
            j++;
        }

        for (j = 0; j < n; j++)
        {
            a[from + j] = b[j];
        }
    } 

     int main() {
        vector<string> v = {"Apple", "Fruit", "Banana", "apple", "4apples", "applesauce", "3bananas", "\"apple\""}

        //  Print original vector
                cout << "******Original*******"<< endl;
                for (vector<string>::size_type i = 0; i < v.size(); ++i)
                {

                  cout << v[i] << endl;
                }

               mergeSort(v, 0, v.size() - 1);

                cout << "******MERGE SORTED*******"<< endl;
                for (vector<string>::size_type i = 0; i < v.size(); ++i)
                {

                      cout << v[i] << endl;
                }

        }

Upvotes: 1

Views: 3235

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 598001

The standard C++ library has its own std::merge() functions. Those are the candidates you see described in the compiler error as not matching. It does not appear the compiler is even considering your merge() function. This implies that you have not declared your function before trying to use it, and that you have a using namespace std statement in your code.

You need to declare your function. And you need to either get rid of the using statement or else declare your function in its own namespace, and then have mergeSort() be explicit about which namespace to pull merge() from.

Upvotes: 4

Coral Kashri
Coral Kashri

Reputation: 3506

Declare the functions before calling them. Add the following line (declaration of merge function), before your mergeSort function's definition.

void merge(vector<string> &a, int from, int mid, int to);

Read about declarations and definitions in C++:

https://www.geeksforgeeks.org/difference-between-definition-and-declaration/

What is the difference between a definition and a declaration?

Upvotes: 1

Related Questions