-1

I'm using vectors and merge sort to find the minimum and maximum and the program keeps throwing errors.

    void mergeSort(vector<int>& numList, vector<int> & minMax, int left, int right){
        int mid;

        if(left < right){
            mid = (left + right) / 2;
            mergeSort(numList, minMax, left, mid);
            mergeSort(numList, minMax, mid + 1, right);
            merge(numList, minMax, left, mid, right);
        }
    }

    void merge(vector<int>& listNum, vector<int> & minMax, int start, int mid, int end){
        int leftPos = start;
        int rightPos = mid + 1;
        int tempPos = start;
        int* tempArr = new int[listNum.size()];

        while(leftPos <= mid && rightPos <= end){
            if(listNum[leftPos] < listNum[rightPos])
                tempArr[tempPos++] = listNum[leftPos++];
            else
                tempArr[tempPos++] = listNum[rightPos++];
        }
        while(leftPos <= mid)
            tempArr[tempPos++] = listNum[leftPos++];
        while(rightPos <= mid)
            tempArr[tempPos++] = listNum[rightPos++];

        minMax.push_back(tempArr[0]); //store min
        minMax.push_back(tempArr[end]); //store max
        delete[] tempArr;
    }

    int main() {
        vector<int> listOfNums;
        vector<int> minAndMax;
        int listOfNumsSize, minAndMaxSize;

        cout<<"Enter a list of unsorted numbers separated by space (end by typing -1): "<<endl;
        listOfNums = readNum ();

        listOfNumsSize = listOfNums.size();

        mergeSort(listOfNums, minAndMax, 0, listOfNumsSize - 1);
        minAndMaxSize = minAndMax.size();

        printNum (minAndMax, minAndMaxSize);

        return 0;
    }

The following are some of the errors I keep getting.

no type named 'value_type' in 'std::__1::iterator_traits<std::__1::vector<int, std::__1::allocator<int> > >'
    typedef typename iterator_traits<_InputIterator1>::value_type __v1;
in instantiation of function template specialization 'std::__1::merge<std::__1::vector<int, std::__1::allocator<int> >, int, int>' requested here
        merge(numList, minMax, left, mid, right);
cdhowie
  • 158,093
  • 24
  • 286
  • 300
Seung
  • 1
  • 2
    The error message looks like you are inadvertently calling [`std::merge()`](https://en.cppreference.com/w/cpp/algorithm/merge) somewhere. This is possible because you are presumably using `using namespace std;` and this is an _excellent_ example of why you should _[never do that](https://stackoverflow.com/q/1452721/501250)._ Remove `using namespace std;` and `std::`-qualify all references to standard library names. – cdhowie Oct 25 '19 at 13:01
  • 1
    Additionally, you are merging into a temporary array, but throwing that away again. So you'll always operate on the unsorted original vector most of the times... – Aconcagua Oct 25 '19 at 13:04
  • Your problem is obviously arising from a name clash due to using namespace. Also you did not poste all definitons, nor headers. Please always post [Minimal complete examples](https://stackoverflow.com/help/minimal-reproducible-example) – Superlokkus Oct 25 '19 at 13:05

1 Answers1

0

The problem is that merge() is defined below mergeSort() in the source file. So when compiling mergeSort() the compiler instead finds std::merge() and then failing to match the arguments.

So move merge() above mergeSort() or forward declare it.

This is a clear case why using namespace std is bad.

Pibben
  • 1,876
  • 14
  • 28