【问题标题】:No matching function for call to "BubbleSort" [closed]调用“BubbleSort”没有匹配的功能[关闭]
【发布时间】:2014-12-28 22:10:19
【问题描述】:

您好,我遇到一个错误问题“没有调用“BubbleSort”的匹配函数。我正在创建一个没有类的 BubbleSort 程序。我的 BubbleSort 中的参数与 main 中的函数调用匹配,所以我不知道为什么我收到此错误。有什么想法吗?

我的主要看起来是这样的:

int main()
{
int size = 5000;

int* array = CreateAnArray(size);

BubbleSort(array, size, comparison, itemAssignment);  ///This is where the error is 
}

BubbleSort 函数如下所示:

int BubbleSort(int* array, int size, int comparison, int itemAssignment)
{
bool done = false;
while (!done) {
    done = true;
    for (int i = 0; i < size - 1; i++) {
        if (array[i] > array[i + 1]) {
            done = false;
            comparison++;
            Swap(array, i, i + 1);   
        }
        else
        {
            itemAssignment++;
        }  
    }
}
cout << "Number of comparisons: " << comparison << "Item Assignments: " << endl;
return comparison;
return itemAssignment;
}

*************我的整个代码*************

#include <iostream>
#include <cstdlib>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int* CreateAnArray(int size) {
    srand((unsigned)time(0));
    int* array = new int[size];
    for (int i = 0; i <size; i++) {
        int randomnum = 1 + rand() % 100;
        array[i] = randomnum;
    }
    return array;
}

void Swap(int* array, int a, int b)
{
    int tmp = array[a];
    array[a] = array[b];
    array[b] = tmp;
}

int BubbleSort(int* array, int size, int comparison, int itemAssignment)
{
    bool done = false;
    while (!done) {
        done = true;
        for (int i = 0; i < size - 1; i++) {
            if (array[i] > array[i + 1]) {
                done = false;
                comparison++;

                Swap(array, i, i + 1);   
            }
            else
            {
                itemAssignment++;
            }
        }
    }
    cout << "Number of comparisons: " << comparison << "Item Assignments: " << endl;
    return comparison;
    return itemAssignment;
    }

int get_comparison(int comparison){

    return comparison;
}

int get_itemAssignment(int itemAssignment){
    return itemAssignment;
}


int main()
{
    int size = 5000;
    int* array = CreateAnArray(size);

    BubbleSort(array, size, comparison, itemAssignment);       
}

【问题讨论】:

  • get_comparisonget_itemAssignment 是什么?如果它们是函数而不是int's 那么这就是你的问题。如果它们不是int 类型,那就有问题了。我以几种不同的方式说明这一点,因为您的参数不匹配,并且您已经忽略了编译器错误。重申一下,您的参数匹配,但您的参数不匹配。
  • BubbleSort 是否在同一个文件中?不同的文件?你能把你的整个代码贴出来吗?
  • @CaptainObvlious 我创建了 get_comparison 和 get_itemAssignment 来获取计数器比较和 itemAssignment 的值,因为 void 函数无法返回值。我应该尝试不同的东西吗?我是 C++ 新手
  • @Barry 是的,但我刚刚为你粘贴了我的整个代码
  • 好的,所以您尝试将函数作为整数传递?不是。去。到。发生。成功。

标签: c++ parameters parameter-passing bubble-sort


【解决方案1】:

比较

BubbleSort(array, size, comparison, itemAssignment);

未定义。

【讨论】:

  • 谢谢!这有帮助。我只需要为计数器添加 int comparison = 0 和 int itemAssignment = 0!
  • 如果你只是做 int BubbleSort(int* array, int size, int comparison = 0, int itemAssignment = 0) 那么你所做的就是为这些参数设置默认值。从现在开始,您可以只使用两个参数调用 BubbleSort:BubbleSort(array, size) 和省略参数的值为 0
猜你喜欢
  • 1970-01-01
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-13
  • 1970-01-01
  • 2014-12-08
  • 1970-01-01
相关资源
最近更新 更多