Quicksort - No matching function for call to c++

房东的猫 提交于 2019-12-25 09:01:11

问题


Im trying to create a quick sort algorithm using pointers and am having a but of trouble

The line: int* pivot = partition(start, stop); is causing the error "No matching function for call to partition". There may be other issues with the code, but this is the one not allowing me to run. Any help would be greatly appreciated.

void quickSort(int* start, int* stop) {
    if (stop - start <= 1) return;
    int* pivot = partition(start, stop);
    quickSort(start, pivot);
    quickSort(pivot + 1, stop);
}

int partition(int* start, int* stop) {
    int* pivot = stop - 1;
    int* i = start;
    int* j = stop - 1;
    for (;;) {
        while (i < pivot && i < stop) ++i;
        while (j >= pivot && j > start) --j;
        if (*i >= *j) break;
        swap(i, j);
    }
    swap(*(stop - 1), *i);
    return *i;
}

回答1:


The compiler does all its work in one pass. So when it sees the usage of "partition" before it's actually declared (or defined), it doesn't know how to interpret that symbol. So it spews an error.

Simple solution is to just forward declare the partition function. Add this line above the quickSort function.

 /* forward declare */
 int partition(int* start, int* stop);

What this basically says to the compiler is, "hey, when you see partition token later in the compile, don't freak out, it's a function that takes two int pointer params. It will be defined later and the linker will take care of resolving it.".




回答2:


Either declare function prototype above

int partition(int *, int *);

or

define partition() above quickSort()




回答3:


The main problem is that pivot must not be an int * but simply a plain int, as you are comparing array values, not array references. you have to change your code in partition function, to show something like:

int pivot = *(stop - 1);
...
if (i < stop && *i <= pivot) ... /* [ NOTE 1 ] */
if (j >= start && *j >= pivot) ... /* [ NOTE 1 ] */

NOTE 1

you have to check first for the array position, before comparing, as you can lead comparing elements that are outside the range. Be most careful when using && operator and double check the order in which you check the clauses (in boolean algebra this is not important, as the AND operator is commutative, but not in C/C++, you have to first check for array boundaries, then the array element, or you'll run into trouble)

NOTE 2

You have to copy apart pivot, as it can be moved as the result of some comparation, and you'll finish using different pivots in each call to partition. The best should be that you get the pivot (as the selection of pivot is something open in the algorithm) from a possible external function call and pass the value directly to partition (well, but this is not related to the problem)

Of course, the problem of the missing forward declaration of partition() is another thing already described in other answers. C++ requires that everything used in the program is properly declared before its use.



来源:https://stackoverflow.com/questions/40102903/quicksort-no-matching-function-for-call-to-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!