调用布尔函数但收到错误"no matching function to call"?

Calling a boolean function but getting error "no matching function to call"?

本文关键字:matching no function to call 错误 函数 布尔 调用      更新时间:2023-10-16

我正在尝试动态声明2D数组,并用随机数填充它们,然后创建一个函数,该函数将在两个2D阵列中比较元素,如果它们相等,它将返回true

但是,我在尝试调用布尔函数时一直遇到错误。

#include <iostream>
#include <cstdlib>
using namespace std;
bool isEqual(int *arr1[], int *arr2[], bool &eq, int row, int col){
for(int r = 0; r<row;r++)
{
    for(int c= 0; c<col;r++)
    {
        if(arr1[r][c]==arr2[r][c])
            eq = true;
    }
}
return eq;
 }
int main()
{
const int R = 3;
int * arr2D_a[R];
int * arr2D_b[R];
int C;
cout << "Enter number of columns: ";
cin >> C;
for (int r = 0; r < R; r++) {
    arr2D_a[r] = new int [C];
    arr2D_b[r] = new int [C];
}

for (int r = 0; r < R; r++) {
    for (int c = 0; c < C; c++) {
        arr2D_a[r][c] = rand() % 2;
        arr2D_b[r][c] = rand() % 2;
    }
}
bool result = false;
isEqual(arr2D_a,arr2D_b,result,R,C);
if (result==true)
    cout << "nThe 2 array are the same!n";
else
    cout << "nThe 2 array are the differernt!n";
for (int c = 0; c < C; c++) {
    delete[] arr2D_a[C];
    delete[] arr2D_b[C];
}
for (int r = 0; r < R; r++)  {
    delete[] arr2D_a[r];
    delete[] arr2D_b[r];
}

system("pause");
}

edit 我掌握了重写您的代码的自由。我发布的代码,在VS2017中编译。

您的比较似乎很

#include <iostream>
#include <cstdlib>
using namespace std;
bool isEqual(int* arr1[], int* arr2[], const int row, const int col) {
    for (int r = 0; r < row; r++)
    {
        for (int c = 0; c < col; c++)
        {
            if (arr1[r][c] != arr2[r][c])
                return false;
        }
    }
    return true;
}
int main()
{
    const int R = 3;
    int * arr2D_a[R];
    int * arr2D_b[R];
    int C;
    cout << "Enter number of columns: ";
    cin >> C;
    for (int r = 0; r < R; r++) {
        arr2D_a[r] = new int[C];
        arr2D_b[r] = new int[C];
    }

    for (int r = 0; r < R; r++) {
        for (int c = 0; c < C; c++) {
            int value = rand();
            arr2D_a[r][c] = value % 2;
            arr2D_b[r][c] = value % 2;
        }
    }
    bool result = isEqual(arr2D_a, arr2D_b, R, C);
    if (result)
        cout << "nThe 2 array are the same!n";
    else
        cout << "nThe 2 array are the differernt!n";
    for (int r = 0; r < R; r++) {
        delete[] arr2D_a[r];
        arr2D_a[r] = 0;
        delete[] arr2D_b[r];
        arr2D_b[r] = 0;
    }
    return 0;
}
  1. 您必须为您的功能正确声明您的参数。 bool iSequal(int arr1,int ** arr2,bool&eq,eq,int row,int col)**,因为您有一个2D数组
  2. 检查值是否差异,请尽快逃脱函数。无需bool变量
  3. 我不知道这是否是故意的,但是您的初始阵列。他们无法匹配。您每次都称为 rand(),因此值无法匹配
  4. 删除列是一件小事。您必须使用索引 c 而不是变量 c
  5. 我没有更改... PLS不使用使用名称空间std; 。这个名称空间是如此巨大。当您定义自己的函数时,当您声明具有存在名称的函数时,您可以遇到不可扣除的错误。

编辑2

我完全删除了函数调用中的布尔...

编辑3

要使您保持良好的保留此程序,您必须提供回报值

另一个错误是,您不得做第二个删除循环。由于您没有动态分配此内存。

编辑4

重新加工了该功能以取悦所有编译器=)

编辑5

我希望它是此答案的最后一个编辑^^我解决了内存问题。我和博士一起检查了。记忆,他说,一切都很好:D

上面的答案解决了大多数问题,但是您会得到segfault她。

for (int c = 0; c < C; c++) {
    delete[] arr2D_a[c];
    delete[] arr2D_b[c];
}

如果您在这里放了大于3的东西

std::cin >> C;

您需要做的是离开第二个循环:

for (int r = 0; r < R; R++) {
    delete[] arr2D_a[r];
    delete[] arr2D_b[r];
}

因为您在每个arr2D_a[r]arr2D_b[r]中分配了C的空间量。

相关文章: