c++ - C++:函数 “no matching function for call to”错误

标签 c++

我在类“MAX_SUB_MAT”中定义了一个函数“mod_kadane”,该函数返回一个对象。

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

但是,当我尝试从“main”调用它时:
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);

然后显示错误:

error: no matching function for call to 'MAX_SUB_MAT::mod_kadane(int [row][column], int&, int&)'|



但是,如果我从两边都删除了2d数组“mat”,则代码可以正常工作。

这是我的完整代码:
#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;
}

最佳答案

在C或C++中,我们不能使用二维数组参数声明两个大小的函数来声明函数。

另请:http://c-faq.com/aryptr/pass2dary.html

@AlanStokes在其评论中已正确解释了该问题!

关于c++ - C++:函数 “no matching function for call to”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37760196/

相关文章:

c++ - 如何将 vector 写入文件以供执行程序时使用? (c++)

c++ - 在 64 位 Windows 上将字符串从 C++ 返回到 C# 时如何防止 AccessViolationException?

c++ - 使用 shared_ptr 从方法返回指针是否总能避免内存泄漏?

c++ - 如何正确使用 `std::vector.back()`?

c++ - 现在要删除与谓词匹配的元素?

c++ - 我想我不明白 makefile 是如何工作的

c++ - 是否可以在父子目录项目中修改 QT qmake 变量?

c++ - 在这种情况下被 lambda 输出混淆

c++ - 发送智能指针到 protobaf。内存问题

c++ - SFML 事件不显示?