【问题标题】:How to pass a two dimensional array parameter of a function as parameter of an inner function如何将函数的二维数组参数作为内部函数的参数传递
【发布时间】:2021-03-27 07:18:22
【问题描述】:

我发现了如何将二维数组作为参数传递给函数,然后对其元素进行操作。然而,我的情况更具体:一个函数有一个二维数组作为参数,它的主体包含另一个函数,其参数是同一个数组。 这是我的代码:

#include <iostream>
using namespace std;
const double FLT_EPSILON=0.3;
bool areEqual(double a, double b, double epsilon)
{
    return abs(a - b) <= ((abs(a) > abs(b) ? abs(b) : abs(a)) * epsilon);
}
double LineSUM(int N, double matrix[][N], int line)
{
    double sum = 0;
    for (size_t i = 0; i < N; i++)
    {
        sum = sum + matrix[line][i];
    }
    return sum;
}
double RowSUM(int N, double matrix[][N], int row)
{
    double sum = 0;
    for (size_t i = 0; i < N; i++)
    {
        sum = sum + matrix[i][row];
    }
    return sum;
}
//some other  irrelevant code
bool matrixIsMagicalSquare(int N, double matrix [N][N])
{
    for (size_t i = 0; i < N; i++)
    {
        for (size_t j = 0; j < N; j++)
        {
            if (!areEqual(LineSUM(N, matrix, i), LineSUM(N, matrix, j), FLT_EPSILON))
            {
                return false;
            }
        }
    }
    //some other code
}
int main()
{
    int N=0;
    cin>>N;
    double martrix[N][N];
    //code to enter the elements of the array
    cout<<matrixIsMagicalSquare(N, matrix);
}

尝试编译代码返回“No matching function for call to 'LineSUM'”和 RowSUM 的相同错误。出现此消息的原因是Line/RowSUM 的参数(称为matrix 的数组)显然与其定义中的类型不同。我究竟做错了什么?请原谅我在问题中的任何不准确之处。英语不是我的母语。

【问题讨论】:

  • 请注意,将 bool matrixIsMagicalSquare(int N, double matrix [N][N]) 更改为 bool matrixIsMagicalSquare(int N, double matrix [][N])bool matrixIsMagicalSquare(int N, double (&amp;matrix)[][N]) 对我不起作用。
  • 上面的代码不是合法的 C++。在 C++ 数组中,边界必须是编译时常量 而不是 变量。无论你在哪里学习 C++,它都没有告诉你真相。
  • 在 C++ 中,如果你想要一个可变大小的数组,你可以使用 std::vector
  • @john。哎呀,我错过了。当我将该代码放在帖子中时,我删除了其中的一些,并且 N 的声明被意外删除了。我将编辑帖子。
  • 谢谢,但重点是您的代码在 C++ 中是不合法的。这是不合法的 C++ int N=0; cin&gt;&gt;N; double martrix[N][N];

标签: c++ function multidimensional-array arguments


【解决方案1】:

问题出在您的函数标题中。括号内的参数值是非法的。更改您的代码以使用数组指针:

double LineSUM(int N, double** matrix, int line)
{
    double sum = 0;
    for (size_t i = 0; i < N; i++)
    {
        sum = sum + matrix[line][i];
    }
    return sum;
}

double RowSUM(int N, double** matrix, int row)
{
    double sum = 0;
    for (size_t i = 0; i < N; i++)
    {
        sum = sum + matrix[i][row];
    }
    return sum;
}

//some other  irrelevant code
bool matrixIsMagicalSquare(int N, double** matrix)
{
    for (size_t i = 0; i < N; i++)
    {
        for (size_t j = 0; j < N; j++)
        {
            if (!areEqual(LineSUM(N, matrix, i), LineSUM(N, matrix, j), FLT_EPSILON))
            {
                return false;
            }
        }
    }
    //some other code
}

int main()
{
    cin>>N;
    double** matrix;
    // You will now have to initialize matrix on the heap because
    // you cannot initialize an array on the stack with a non-const size.

    //code to enter the elements of the array
    cout<<matrixIsMagicalSquare(N, matrix);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-17
    • 2022-07-25
    • 1970-01-01
    相关资源
    最近更新 更多