user10798572
user10798572

Reputation: 71

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

I am trying to declare 2d arrays dynamically and fill them with random numbers and then create a function that will compare the elements in two 2d arrays and if they were equal it will return true

However, i keep getting error when trying to call the boolean function.

#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");
}

Upvotes: 2

Views: 1210

Answers (2)

skratchi.at
skratchi.at

Reputation: 1151

EDIT i took the liberty of rewriting your code. the code i posted, compiled in VS2017.

your compare seems kind off

#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. you have to declare your parameters for your function right. bool isEqual(int arr1, int** arr2, bool &eq, int row, int col)** because you have a 2D array
  2. check if the values diff, escape the function as soon as they do. there is no need for a bool variable
  3. i dont know if it was intentional, but your init of the arrays. there was no way that they could have matched. you called rand() everytime, so the values can't match
  4. was a little thing with the delete of the columns. you have to use your index c not the variable C
  5. this i did not change... pls don't use using namespace std;. this namespace is so enormously huge. when you define your own functions, you can run into undebugable errors, when you declare a function with a name that exists.

EDIT 2

I totally removed the bool in the function call...

EDIT 3

to leave this program for good you have to provide a return value

another mistake was, you must not make the second delete loop. since you have not dynamically allocated this memory.

EDIT 4

reworked the function to please all the compilers =)

EDIT 5

i hope its the last edit for this answer^^ i fixed the memory issue. i checked it with dr. memory and he says, everything is ok :D

Upvotes: 4

user5550963
user5550963

Reputation:

The answer above fixes most of the issues, but you will get segfault her.

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

}

if you put something greater than 3 up here

std::cin >> C;

What you need to do is leave the second loop:

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

because you allocated C amount of space in every arr2D_a[r] and arr2D_b[r].

Upvotes: 2

Related Questions