【问题标题】:How to define a function, parameters of which take variables from main() function?如何定义一个函数,其参数从 main() 函数中获取变量?
【发布时间】:2020-11-14 06:43:20
【问题描述】:

我遇到了定义一个函数的需求,它的参数从 main() 函数中获取参数。代码如下:

#include <iostream>
using namespace std;

void unravel(char arr[][column], int field[][column], int x, int y) { //revealing adjoined cells
    for (int minusrows = -1; minusrows < 2; minusrows++){
        for (int minuscolumns = -1; minuscolumns < 2; minuscolumns++){
            arr[x + minusrows][y + minuscolumns] = field[x + minusrows][y + minuscolumns] + '0';
        }
    }
}

int main (){
    
    int row, column;
    cin >> row >> column;

    char a[row][column];
    int field[row][column];
    
    for (int i = 0; i < row; i++){ //filling a with asterisks, field with integers
        for (int j = 0; j < column; j++){
            a[i][j] = '*';
            field[i][j] = i + j;
        }
    }
 
    int x = 2, y = 3;
    
    unravel(a, field, x, y);
    
    for (int i = 0; i < 5; i++){ //printing out the array a
        for (int j = 0; j < 5; j++){
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
  
    return 0;
}

这是我得到的错误:

main.cpp:4:25: error: ‘column’ was not declared in this scope
 void unravel(char arr[][column], int field[][column], int x, int y) {
                         ^~~~~~
main.cpp:4:32: error: expected ‘)’ before ‘,’ token
 void unravel(char arr[][column], int field[][column], int x, int y) {
                                ^
main.cpp:4:34: error: expected unqualified-id before ‘int’
 void unravel(char arr[][column], int field[][column], int x, int y) {
                                  ^~~

因此,我们的目标是将最初由星号 (*) 组成的 2D 数组 a[ ][ ] 中的一个单元格 围绕 的值更改为 int 2D 数组字段 [ ][ 的值] 的相同指标。为此,函数 unravel() 负责。当我使用整数 5 而不是 rowcolumn 时,代码运行良好。

#include <iostream>
using namespace std;

void unravel(char arr[][5], int field[][5], int x, int y) { //revealing adjoined cells
    for (int minusrows = -1; minusrows < 2; minusrows++){
        for (int minuscolumns = -1; minuscolumns < 2; minuscolumns++){
            arr[x + minusrows][y + minuscolumns] = field[x + minusrows][y + minuscolumns] + '0';
        }
    }
}

int main (){
    
    char a[5][5];
    int field[5][5];
    
    for (int i = 0; i < 5; i++){ //filling a with asterisks, field with integers
        for (int j = 0; j < 5; j++){
            a[i][j] = '*';
            field[i][j] = i + j;
        }
    }
 
    int x = 2, y = 3;
    
    unravel(a, field, x, y);
    
    for (int i = 0; i < 5; i++){ //printing out the array a
        for (int j = 0; j < 5; j++){
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
  
    return 0;
}

因此问题是:是否可以从 main() 函数中存储一些数据,以便函数 unravel() 可以按我的预期工作?我知道变量是在范围内定义的。我看到的关于我得到的错误的任何其他内容都与我的问题没有太大关系。对不起,如果帖子看起来很长。提前致谢。

【问题讨论】:

  • char a[row][column];int field[row][column]; -- 这不是有效的 C++。 C++ 数组的大小必须由常量表达式表示,而不是运行时变量。 C++ 中的动态数组是使用std::vector 完成的

标签: c++ arrays function


【解决方案1】:

main() 中的这些行:

int row, column;
cin >> row >> column;

char a[row][column];
int field[row][column];

导致尝试使用运行时值声明数组。 This is not valid C++.

C++ 中的数组的大小必须由编译时表达式表示,而不是运行时值。

最简单的解决方案是使用std::vector。使用typedefusing 可以简化这一点:

#include <vector>

using Char1D = std::vector<char>;
using Char2D = std::vector<Char1D>;
using Int1D = std::vector<int>;
using Int2D = std::vector<Int1D>;

int main()
{
    int row, column;
    cin >> row >> column;

    Char2D a(row, Char1D(column,'*'));
    Int2D field(row, Int1D(column));

    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < column; j++)
            field[i][j] = i + j;
    }
    //...
    int x = 2, y = 3;

    unravel(a, field, x, y);
}

那么unravel函数,在使用这些定义的时候,也变得更容易处理了:

void unravel(Char2D& arr, Int2D& field, int x, int y)
{ 
    //... 
}

请注意,如果您实际使用 2D 数组,我们会传递引用,模仿行为。

基本上其他一切都保持不变。

【讨论】:

    【解决方案2】:

    动态二维数组可以像这样声明为指向数组的指针数组

    int** a = new int*[rowCount];
    for(int i = 0; i < rowCount; ++i)
        a[i] = new int[colCount];
    

    更多详情请参考:How do I declare a 2d array in C++ using new?

    因此,如果您使用 new 关键字声明二维数组,您的代码将可以正常工作。

    #include <iostream>
    using namespace std;
    
    void unravel(char **arr, int **field, int x, int y) { //revealing adjoined cells
        for (int minusrows = -1; minusrows < 2; minusrows++){
            for (int minuscolumns = -1; minuscolumns < 2; minuscolumns++){
                arr[x + minusrows][y + minuscolumns] = field[x + minusrows][y + minuscolumns] + '0';
            }
        }
    }
    
    int main (){
        
        int row, column;
        cin >> row >> column;
    
        char **a = new char*[row];
        int **field = new int*[row];
        
        for (int i = 0; i < row; i++){ //filling a with asterisks, field with integers
            a[i]=new char[column];
            field[i] = new int[column];
            for (int j = 0; j < column; j++){
                a[i][j] = '*';
                field[i][j] = i + j;
            }
        }
     
        int x = 2, y = 3;
        
        unravel(a, field, x, y);
        
        for (int i = 0; i < row; i++){ //printing out the array a
            for (int j = 0; j < column; j++){
                cout << a[i][j] << " ";
            }
            cout << endl;
        }
        
        for(int i=0; i<row; i++){
            delete [] a[i];
            delete [] field[i];
        }
        
        delete [] a;
        delete [] field;
        
        return 0;
    }
    

    请注意:内存是使用 new 关键字在堆上分配的。为了防止内存泄漏,我们应该在最后释放分配的内存。在这个程序中可以这样做。

    for(int i=0; i<row; i++){
        delete [] a[i];
        delete [] field[i];
    }
    
    delete [] a;
    delete [] field;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-28
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      • 1970-01-01
      相关资源
      最近更新 更多