调用布尔函数但收到错误"no matching function to call"?
Calling a boolean function but getting error "no matching function to call"?
我正在尝试动态声明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;
}
- 您必须为您的功能正确声明您的参数。 bool iSequal(int arr1,int ** arr2,bool&eq,eq,int row,int col)**,因为您有一个2D数组
- 检查值是否差异,请尽快逃脱函数。无需bool变量
- 我不知道这是否是故意的,但是您的初始阵列。他们无法匹配。您每次都称为 rand(),因此值无法匹配
- 删除列是一件小事。您必须使用索引 c 而不是变量 c
- 我没有更改... 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
的空间量。
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- "no matching function for call to 'Vector::Vector'"错误
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 为什么当我在 std::string 中打开文件名的 fstream 时出现"no matching function"错误?
- 从 TVector2 类派生复杂类:"No matching function for call to Complex::Complex()"
- 学习 c++,编译错误"no matching function for call"
- C++ "error: no matching function"
- 尝试实例化模板类的对象时出现"No Matching Constructor"错误
- 结构错误:"no matching function for call to "
- 创建函数变体向量时"No matching function for call"错误
- 为什么"std::isdigit"在 Windows 上报告C++"no matching overloaded function"
- 编译时二叉搜索树错误的反向迭代器表示"no matching function call for operator=()"
- 如果子类中没有构造函数方法,则错误"no matching function for call to 'LGame::LGame(String&)'"
- 提升多精度浮点数128:标准::exp错误:'no matching function for call'
- "error: no matching function for call to",我无法更改结构的定义
- 替换派生对象向量中的对象"no matching function to call"
- 我的程序显示许多错误,"error: no matching function for call to 'getPercentScore'"
- 为什么即使我定义了它们"no matching overload found"也会出现错误
- 为什么有一个指向此代码行(第 7 行)的"no matching call for function"错误?