-2

I am trying to write this code to count the number of inversions in an array using merge sort technique. I took reference from GeeksForGeeks. Even though I am not explicitly playing with pointers I am getting this error.

error: invalid type argument of unary ‘*’ (have ‘int’)

Below is my code.

#include <bits/stdc++.h>
using namespace std;

int mergeSort(int arr[], int temp[], int l, int r){
    int invcount = 0;

    if(l<r){
        int m = (l+r)/2;
        invcount += mergeSort(arr, temp, l, m);
        invcount += mergeSort(arr, temp, m+1, r);

        invcount += merge(arr, temp, l, m, r);
    }
    return invcount;
}

int merge(int arr[], int temp[], int l, int m, int r){
    int invcount = 0;
    int k = l;
    int i = l, j = m+1;

    while(true){
        if(i>m){
            while(j<=r){
                temp[k] = arr[j];
                k++;
                j++;
            }
            break;
        }
        else if(j>r){
            while(i<=m){
                temp[k] = arr[i];
                k++;
                i++;
            }
            break;
        }

        if(arr[i] > arr[j]){
            temp[k] = arr[j];
            k++;
            j++;
            invcount += m-i+1;
        }
        else{
            temp[k] = arr[i];
            k++;
            i++;
        }
    }

    for(i=l; i<=r; i++){
        arr[i] = temp[i];
    }
    return invcount;
}

int main(int argv, char** args)
{
    // int arr[] = {1, 20, 6, 4, 5};
    int arr[] = {2, 4, 1, 3, 5};
    int n = sizeof(arr)/sizeof(arr[0]);
    int *temp = new int[n];
    printf(" Number of inversions are %d \n", mergeSort(arr, temp, 0, n-1));
    // getchar();
    return 0;
}

Can someone please help me out?

  • 2
    can you tag the line at witch the error happens – Tyker Jun 22 '18 at 11:47
  • 5
    `#include ` -- [Don't do this](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). Include the correct headers. – PaulMcKenzie Jun 22 '18 at 11:51
  • 1
    Better to read [these C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Ron Jun 22 '18 at 11:53
  • 2
    The issue is more than likely about using that stupid "bits" header instead of the real headers and messing up the `merge` function. How about showing us where the error occurs? – PaulMcKenzie Jun 22 '18 at 11:54
  • 1
    Compiles and runs on my machine. No error. Touched up the code to fix a few warnings, so compiled without warnings. – Eljay Jun 22 '18 at 11:55
  • @Eljay -- No, don't touch up anything. Take the code as-is and attempt to compile it. – PaulMcKenzie Jun 22 '18 at 11:58

1 Answers1

2

The issue is more than likely your usage of #include <bits/stdc++.h> and calling merge.

The problem is that there is a std::merge function defined in the <algorithm> header, and the <bits/stdc++.h> header includes <algorithm>. In addition, you have

using namespace std;

complicating the issue further. You're actually trying to call std::merge instead of your own merge function, all due to the std namespace being brought into your program. The std::merge function requires different argument types than the ones you are passing to your own merge function, thus the error.


The solution -- stop using #include <bits/stdc++.h>. Include the proper, standard C++ header instead of this one. The proper headers would be:

#include <cstdio>

Then you would (or should) encounter another error, that merge is being called, but it is not known to the compiler. In that case, you either declare the merge function and place that before the mergeSort function

// declare the merge() function here
int merge(int arr[], int temp[], int l, int m, int r);

int mergeSort(int arr[], int temp[], int l, int r)
{
   int invcount = 0;
   if(l<r)  {
     int m = (l+r)/2;
     invcount += mergeSort(arr, temp, l, m);
     invcount += mergeSort(arr, temp, m+1, r);
     invcount += merge(arr, temp, l, m, r);
  }
  return invcount;
}

// implement merge() here
int merge(int arr[], int temp[], int l, int m, int r)
{
   //...
}

or move the entire merge function implementation before the mergeSort function.

PaulMcKenzie
  • 31,493
  • 4
  • 19
  • 38
  • Thanks a lot, @PaulMcKenzie. It was exactly the same issue you stated. The program was calling `std::merge` instead of my own merge. I checked it up by changing the function name and it is working fine. I guess I should better stop using the "bits" header and start using the correct headers. Thanks again. – Abhijeet Bhure Jun 22 '18 at 13:43
  • And that was a silly mistake of not declaring the merge function before mergeSort. :p – Abhijeet Bhure Jun 22 '18 at 13:49