我有一个功能" mod_kadane"在课堂上定义" MAX_SUB_MAT"返回一个对象。
#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 i = 0; i < 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;
}
};
但是当我试图从&#34; main&#34;
中调用它时cin >> row >> column;
int mat[row][column];
for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
cin >> mat[i][j];
}
}
MAX_SUB_MAT objx;
objx = objx.mod_kadane(mat, row, column);
然后会显示错误:
错误:没有匹配函数来调用&#39; MAX_SUB_MAT :: mod_kadane(int [row] [column],int&amp;,int&amp;)&#39; |
但如果我删除了2d数组&#34; mat&#34;从双方来看,代码工作正常。
这是我的完整代码:
#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 i = 0; i < n; i++)
cout << arr[i] << endl;*/
for(int i = 0; i < 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 i = 0; i < 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 you want to insert?:\n");
cin >> row;
printf("Number of columns you want to insert?:\n");
cin >> column;
int mat[row][column];
if(row && column)
{
for(int i = 0; i < 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 i = objx.up; i <= objx.down; i++)
{
for(int j = objx.left; j <= objx.right; j++)
{
cout << mat[i][j] << " ";
}
cout << endl;
}
}
return 0;
}
答案 0 :(得分:0)
在C或C ++中,我们不能声明具有指定两种尺寸的二维数组参数的函数。
请参阅:http://c-faq.com/aryptr/pass2dary.html
@AlanStokes在评论中正确解释了这个问题!