簡體   English   中英

將 Arrays 數組傳遞給 C++ 中的其他函數的標准方法失敗

[英]Standard Ways Of Passing Array of Arrays To Other Functions in C++ Failing

我試圖將一個二維整數數組從我的主 function 在 cpp 程序中傳遞給另一個 function,並在另一個 ZC1C425268E68385D1AB5074C7A 中操作二維數組。 雖然我以前做過,但已經有一段時間了,所以我遵循了這個公認的答案:

直接鏈接到有問題的答案下面的程序直接模仿

然而,雖然對我來說一切都很好,但答案中建議的方法中有 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);
}

此外,當使用建議的第 3 種方法時,我不確定賦值是如何工作的,甚至不確定如何訪問數組中的值。

我已經粘貼了 clang 編譯器在對第二個 function 的每次調用上方的注釋中給出的錯誤。 除了main之外的函數沒有錯誤,這些是直接從上面鏈接的答案中獲取的。 但是,我也以與上述鏈接為每種方法建議的相同方式傳遞了二維數組,所以我真的不知道這里出了什么問題。

數組在表達式中使用時衰減為指向其第一個元素的指針。

因此int [LINES_IN_FILE][NUMS_PER_LINE]類型的變量衰減為int (*)[NUMS_PER_LINE]類型,而后者在 function 聲明中也可以表示為int [][NUMS_PER_LINE]

所以你想用這個function:

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

並直接傳遞數組:

change2dArrayMethod2(coordLines);

此外,C++ 不允許數組索引使用非常量值。 您需要將值設為const

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM