[英]Trying to create a function that passes a 2d array outputs array in a table format (similar to tabulate from python3)
所以我正在嘗試創建一個 function ,它將二維數組作為輸入以及行和列變量以及 output 以表格格式的數組內容。 這是我到目前為止所擁有的
#include <iostream>
using namespace std;
void Tabulate(int *x[][], int xRows, int xCols) {
for (int i = 0; i < xRows; i++) {
for (int j = 0; j < xCols; j++) {
cout << x[i][j] << "\t";
}
cout << endl;
}
}
int main() {
int rows = 2;
int cols = 3;
int x[rows][cols] = {{2,3,4}, {8,9,10}};
Tabulate(x, rows, cols);
}
這是它返回的錯誤
tabulate.cpp:4:20: error: declaration of ‘x’ as multidimensional array must have bounds for all dimensions except the first
4 | void Tabulate(int *x[][], int xRows, int xCols) {
| ^
tabulate.cpp:4:25: error: expected ‘)’ before ‘,’ token
4 | void Tabulate(int *x[][], int xRows, int xCols) {
| ~ ^
| )
tabulate.cpp:4:27: error: expected unqualified-id before ‘int’
4 | void Tabulate(int *x[][], int xRows, int xCols) {
| ^~~
make: *** [<builtin>: tabulate] Error 1
我知道這與定義數組第二維的語法有關,但我無法為我的具體情況找到任何東西。 抱歉,如果我錯過了一些愚蠢的東西,但我會很感激幫助:/。
您正在嘗試將二維數組傳遞給 function。 在這種情況下,您的數組應該是動態的或一維常量。
用這個:
void Tabulate(int **x, int xRows, int xCols) //dynamic array
在此處查看更多詳細信息/方法(此處有一些非常好的答案): Passing a 2D array to a C++ function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.