c++ : "no matching function for call to" error with function -
i have function "mod_kadane" defined in class "max_sub_mat" returns object.
#define dx 101 class max_sub_mat { public: int curr_sum, max_sum, left, right, up, down; max_sub_mat mod_kadane(int mat[dx][dx], int row, int column) { max_sub_mat objx; curr_sum = objx.max_sum = int_min; int sub_mat[row]; for(int l = 0; l < row; l++) { memset(sub_mat, 0, sizeof sub_mat); for(int r = l; r < column; r++) { for(int = 0; < row; i++) { sub_mat[i] += i;//mat[i][r]; } max_sub_arr obj; obj = obj.kadane(sub_mat, row); curr_sum = obj.maxima; if(curr_sum > objx.max_sum) { objx.max_sum = curr_sum; objx.left = l; objx.right = r; objx.up = obj.left; objx.down = obj.right; } } } return objx; } };
but when i'm trying call "main":
cin >> row >> column; int mat[row][column]; for(int = 0; < row; i++) { for(int j = 0; j < column; j++) { cin >> mat[i][j]; } } max_sub_mat objx; objx = objx.mod_kadane(mat, row, column);
then error shown:
error: no matching function call 'max_sub_mat::mod_kadane(int [row][column], int&, int&)'|
but if remove 2d array "mat" both sides code works fine.
here full code:
#include<bits/stdc++.h> #define dx 101 using namespace std; class max_sub_arr { public: int max_curr, maxima, left, right; max_sub_arr kadane(int arr[], int n) { max_sub_arr obj; obj.maxima = max_curr = int_min; //cout << obj.maxima << endl; /*for(int = 0; < n; i++) cout << arr[i] << endl;*/ for(int = 0; < n; i++) { if(max_curr < 0) { max_curr = arr[i]; obj.left = i; } else { max_curr += arr[i]; } //maxima = max(maxima, max_curr); if(max_curr > obj.maxima) { obj.maxima = max_curr; obj.right = i; //cout << obj.maxima << endl; } } return obj; } }; class max_sub_mat { public: int curr_sum, max_sum, left, right, up, down; /* max_sub_mat(int r, int c) { row = r; column = c; }*/ max_sub_mat mod_kadane(int mat[dx][dx], int row, int column) { max_sub_mat objx; curr_sum = objx.max_sum = int_min; int sub_mat[row]; for(int l = 0; l < row; l++) { memset(sub_mat, 0, sizeof sub_mat); for(int r = l; r < column; r++) { for(int = 0; < row; i++) { sub_mat[i] += i;//mat[i][r]; } max_sub_arr obj; obj = obj.kadane(sub_mat, row); curr_sum = obj.maxima; if(curr_sum > objx.max_sum) { objx.max_sum = curr_sum; objx.left = l; objx.right = r; objx.up = obj.left; objx.down = obj.right; } } } return objx; } }; int main() { int row, column; printf("number of rows want insert?:\n"); cin >> row; printf("number of columns want insert?:\n"); cin >> column; int mat[row][column]; if(row && column) { for(int = 0; < row; i++) { for(int j = 0; j < column; j++) { cin >> mat[i][j]; } } //int curr_sum, max_sum, left, right, up, down; //max_sub_mat objx = new max_sub_mat(row, column); max_sub_mat objx; objx = objx.mod_kadane(mat, row, column); for(int = objx.up; <= objx.down; i++) { for(int j = objx.left; j <= objx.right; j++) { cout << mat[i][j] << " "; } cout << endl; } } return 0; }
it not true in c or c++ cannot declare function 2 dimensional array parameter both sizes specified.
see: http://c-faq.com/aryptr/pass2dary.html
the problem has been correctly explained @alanstokes in comments!
Comments
Post a Comment