Quicksort-没有匹配的函数可以调用C ++ [英] Quicksort - No matching function for call to c++

查看:143
本文介绍了Quicksort-没有匹配的函数可以调用C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用指针创建快速排序算法,但遇到了麻烦

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

行:int* pivot = partition(start, stop);导致错误没有匹配的函数调用分区".代码可能还有其他问题,但这是不允许我运行的问题. 任何帮助将不胜感激.

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;
}

推荐答案

编译器一次完成所有工作.因此,当它在实际声明(或定义)之前看到分区"的用法时,它不知道如何解释该符号.因此它会产生一个错误.

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.

简单的解决方案是只向前声明partition函数.在quickSort函数的上方 处添加此行.

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);

这基本上对编译器说的是",嘿,当您稍后在编译中看到partition标记时,请不要惊慌,它是一个需要两个int指针参数的函数.它将被定义稍后,链接器将解决该问题.".

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.".

这篇关于Quicksort-没有匹配的函数可以调用C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆