繁体   English   中英

尝试创建一个 function 以表格格式传递二维数组输出数组(类似于 python3 中的制表)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM