【问题标题】:Standard Ways Of Passing Array of Arrays To Other Functions in C++ Failing将数组数组传递给 C++ 中其他函数的标准方法失败
【发布时间】:2022-01-11 19:18:59
【问题描述】:

我试图将一个二维整数数组从我在 cpp 程序中的主函数传递给另一个函数,并在这个另一个函数中操作二维数组。虽然我以前做过,但已经有一段时间了,所以我一直在关注这个公认的答案:

Direct link to answer in question the below program is modeled directly after

但是,尽管对我来说一切正常,但答案中建议的方法中有 2/3 都失败了。我已经删除了与我在下面粘贴的错误无关的任何内容,希望可以很容易地理解我的意思。

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int LINES_IN_FILE = 500;
int NUMS_PER_LINE = 4;

void change2dArrayMethod1(int (*lines)[LINES_IN_FILE][NUMS_PER_LINE]) {
    (* lines)[0][0] = 1;
    (* lines)[0][1] = 2;
    (* lines)[0][2] = 3;
    (* lines)[0][3] = 4;
}

void change2dArrayMethod2(int lines[][NUMS_PER_LINE]) {
    lines[0][0] = 1;
    lines[0][1] = 2;
    lines[0][2] = 3;
    lines[0][3] = 4;
}

void change2dArrayMethod3(int lines[]) {
    lines[0] = 1; //not sure how to access entire array here
}

int main() {
    int coordLines[LINES_IN_FILE][NUMS_PER_LINE];

    // METHOD 1
    // Fails with error:
    // Cannot initialize a variable of type 'int (*)[LINES_IN_FILE][NUMS_PER_LINE]' 
    // with an rvalue of type 'int (*)[LINES_IN_FILE][NUMS_PER_LINE]'clang(init_conversion_failed)
    int (*p1_coordLines)[LINES_IN_FILE][NUMS_PER_LINE] = &coordLines;
    // Fails with error:
    // No matching function for call to 'change2dArrayMethod1'clang(ovl_no_viable_function_in_call)
    // test.cpp(10, 6): Candidate function not viable: no known conversion from 'int (*)[LINES_IN_FILE][NUMS_PER_LINE]' to 
    // 'int (*)[LINES_IN_FILE][NUMS_PER_LINE]' for 1st argument
    change2dArrayMethod1(p1_coordLines);

    // METHOD 2
    // Fails with error:
    // Cannot initialize a variable of type 'int (*)[NUMS_PER_LINE]' with an lvalue of type 'int [LINES_IN_FILE][NUMS_PER_LINE]'clang(init_conversion_failed)
    int (*p2_coordLines)[NUMS_PER_LINE] = coordLines;
    // Fails with error:
    // No matching function for call to 'change2dArrayMethod2'clang(ovl_no_viable_function_in_call)
    // test.cpp(17, 6): Candidate function not viable: no known conversion from 'int (*)[NUMS_PER_LINE]' to 'int (*)[NUMS_PER_LINE]' for 1st argument
    change2dArrayMethod2(p2_coordLines);

    // METHOD 3
    // Doesn't fail - however not sure how to manipulate array in function called
    int *p3_coordLines = coordLines[0];
    change2dArrayMethod3(p3_coordLines);
}

此外,当使用建议的第三种方法时,我不确定赋值是如何工作的,甚至不确定如何访问数组中的值。

我已将 clang 编译器在 cmets 中给出的错误粘贴到对第二个函数的每次调用上方。除了main之外的功能没有错误,这些是直接从上面链接的答案中获取的。但是,我也以与​​上述链接为每种方法建议的相同方式传递了二维数组,所以我真的不知道这里出了什么问题。

【问题讨论】:

  • 除非你特别喜欢投反对票,否则不要用 C 标记 C++ 问题。
  • LINES_IN_FILENUMS_PER_LINE 需要是 constexpr
  • 代码不是有效的 C++,因为 LINES_IN_FILENUMS_PER_LINE 不是常量表达式。大写名称不会使它们成为常量。
  • 为什么不使用 std::vector?

标签: c++ arrays pointers


【解决方案1】:

在表达式中使用的数组衰减为指向其第一个元素的指针。

所以int [LINES_IN_FILE][NUMS_PER_LINE]类型的变量衰减为int (*)[NUMS_PER_LINE]类型,而后者在函数声明中也可以表示为int [][NUMS_PER_LINE]

所以你想使用这个功能:

void change2dArrayMethod2(int lines[][NUMS_PER_LINE]) {

并直接传递数组:

change2dArrayMethod2(coordLines);

此外,C++ 不允许数组索引使用非常量值。您需要将值设为const:

const int LINES_IN_FILE = 500;
const int NUMS_PER_LINE = 4;

【讨论】:

    猜你喜欢
    • 2021-02-21
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    相关资源
    最近更新 更多