【问题标题】:How can I solve the error -- error: invalid types ‘int[int]’ for array subscript?如何解决错误 - 错误:数组下标的无效类型“int [int]”?
【发布时间】:2022-01-18 04:18:07
【问题描述】:
#include <iostream>
#include <iomanip>
using namespace std;
int col=10;
int row=0;
void avg(int * ar,int row, int col)
{ 
    float size= row * col;
    int sum=0, ave;
    for(int i=0; i<row; i++)
    {
        for(int j=0; j<col; j++){
        
        sum+=ar[i][j];
        cout<<sum;}
        
    }
    ave=sum/size;
    cout<<sum<<endl;
    cout<<ave;

}

int main()
{
    int row, col;
    cout<<"How many rows does the 2D array have: ";
    cin>>row;
    cout<<"How many columns does the 2D array have: ";
    cin>>col;
    int ar[row][col];
    cout<<"Enter the 2D array elements below : \n";
    for(int i=0; i<row; i++){
        cout<<"For row "<<i + 1<<" : \n";
        for(int j=0; j<col; j++)
        cin>>ar[i][j];
    }
    cout<<"\n Array is: \n";
    for(int i=0; i<row; i++)
    {
        for(int j=0; j<col; j++)
        
            cout<<setw(6)<<ar[i][j];
        
        cout<<endl;
        
    }
  
    cout<<"\nAverage of all the elements of the given D array is: \n";
    avg((int*)ar,row,col);
    
    return 0;
}

您好,我编写了这段代码来计算二维数组元素的平均值。尝试访问第 12 -13 行 ar[i][j]

的二维数组的数组元素时出现错误

错误说-错误:数组下标的类型无效'int [int]'

我该如何解决这个错误?

PS:我想在函数参数中给出行(二维数组中的行数)和列(二维数组中的列数),以使其更具动态性。

【问题讨论】:

  • int ar[row][col]; 不是有效的 C++,因为 rowcol 值必须在编译时知道。
  • 到那时我能给出一些虚拟值吗?

标签: c++ arrays error-handling syntax


【解决方案1】:

您的函数参数arint*。但是当你写sum+=ar[i][j] 时,你正在给它下标,就好像我们有一个二维数组一样。您只能为它下标一维,例如arr[i]

另外rowcol常量表达式。在标准 C++ 中,数组的大小必须是编译时常量(常量表达式)。所以,

int ar[row][col]; //this statement is not standard c++

以上陈述不是标准c++

更好的方法(避免这些问题)是使用 2D std::vector 而不是 2D 数组,如下所示。

#include <iostream>
#include <iomanip>
#include <vector>

//this function takes a 2D vector by reference and returns a double value
double avg(const std::vector<std::vector<int>> &arr)
{ 
    int sum=0;
    for(const std::vector<int> &tempRow: arr)
    {
        for(const int &tempCol: tempRow){
        
        sum+=tempCol;
        //std::cout<<sum;
            
        }
        
    }
    
    return (static_cast<double>(sum)/(arr.at(0).size() * arr.size()));
  

}

int main()
{
    int row, col;
    std::cout<<"How many rows does the 2D array have: ";
    std::cin>>row;
    std::cout<<"How many columns does the 2D array have: ";
    std::cin>>col;
    
    //create a 2D vector instead of array
    std::vector<std::vector<int>> ar(row, std::vector<int>(col));
    
    std::cout<<"Enter the 2D array elements below : \n";
    for(auto &tempRow: ar){
        
        for(auto &tempCol: tempRow){
        std::cin>>tempCol;
        }
    }
    std::cout<<"\n Array is: \n";
    for(auto &tempRow: ar)
    {
        for(auto &tempCol: tempRow)
        
            std::cout<<std::setw(6)<<tempCol;
        
        std::cout<<std::endl;
        
    }
  
    std::cout<<"\nAverage of all the elements of the given D array is: \n";
    std::cout<<avg(ar);
    
    return 0;
}

以上程序的输出可见here

【讨论】:

    【解决方案2】:

    您遇到问题的原因是您尝试动态分配一个二维数组(而不是静态)。

    静态分配是指在代码运行之前,[row] 和 [column] 的值被指定。 示例:int ar[35][35] 这将在编译时创建一个 35x35 的二维数组。

    动态分配是指在运行时(编译后,运行时)改变 [row] 和 [column] 的值。这将需要在 C++ 中使用 new 语句在函数 avg 的开头创建一个指定大小的新数组。 C++中数组动态分配的资料很多,比如这篇帖子here

    【讨论】:

      猜你喜欢
      • 2015-09-26
      • 2013-02-12
      • 1970-01-01
      • 2021-05-04
      • 1970-01-01
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多