C++ 错误:数组下标的无效类型“int[int]”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6165449/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:36:00  来源:igfitidea点击:

c++ error: invalid types 'int[int]' for array subscript

c++

提问by Rick

Trying to learn C++ and working through a simple exercise on arrays.

尝试学习 C++ 并完成有关数组的简单练习。

Basically, I've created a multidimensional array and I want to create a function that prints out the values.

基本上,我创建了一个多维数组,我想创建一个打印出值的函数。

The commented for-loop within Main() works fine, but when I try to turn that for-loop into a function, it doesn't work and for the life of me, I cannot see why.

Main() 中的注释 for 循环工作正常,但是当我尝试将该 for 循环转换为函数时,它不起作用,并且在我的一生中,我不明白为什么。

#include <iostream>
using namespace std;


void printArray(int theArray[], int numberOfRows, int numberOfColumns);

int main()
{

    int sally[2][3] = {{2,3,4},{8,9,10}};

    printArray(sally,2,3);

//    for(int rows = 0; rows < 2; rows++){
//        for(int columns = 0; columns < 3; columns++){
//            cout << sally[rows][columns] << " ";
//        }
//        cout << endl;
//    }

}

void printArray(int theArray[], int numberOfRows, int numberOfColumns){
    for(int x = 0; x < numberOfRows; x++){
        for(int y = 0; y < numberOfColumns; y++){
            cout << theArray[x][y] << " ";
        }
        cout << endl;
    }
}

回答by Ben Voigt

C++ inherits its syntax from C, and tries hard to maintain backward compatibility where the syntax matches. So passing arrays works just like C: the length information is lost.

C++ 从 C 继承了它的语法,并在语法匹配的地方努力保持向后兼容性。所以传递数组就像 C 一样:长度信息丢失了。

However, C++ does provide a way to automatically pass the length information, using a reference (no backward compatibility concerns, C has no references):

但是,C++ 确实提供了一种使用引用自动传递长度信息的方法(没有向后兼容性问题,C 没有引用):

template<int numberOfRows, int numberOfColumns>
void printArray(int (&theArray)[numberOfRows][numberOfColumns])
{
    for(int x = 0; x < numberOfRows; x++){
        for(int y = 0; y < numberOfColumns; y++){
            cout << theArray[x][y] << " ";
        }
        cout << endl;
    }
}

Demonstration: http://ideone.com/MrYKz

演示:http: //ideone.com/MrYKz

Here's a variation that avoids the complicated array reference syntax: http://ideone.com/GVkxk

这是一种避免复杂数组引用语法的变体:http: //ideone.com/GVkxk

If the size is dynamic, you can't use either template version. You just need to know that C and C++ store array content in row-major order.

如果大小是动态的,则不能使用任一模板版本。您只需要知道 C 和 C++ 以行优先顺序存储数组内容。

Code which works with variable size: http://ideone.com/kjHiR

适用于可变大小的代码:http: //ideone.com/kjHiR

回答by Frédéric Hamidi

Since theArrayis multidimensional, you should specify the bounds of all its dimensions in the function prototype (except the first one):

由于theArray是多维的,您应该在函数原型中指定其所有维度的边界(第一个除外):

void printArray(int theArray[][3], int numberOfRows, int numberOfColumns);

回答by wiredolphin

I'm aware of the date of this post, but just for completeness and perhaps for future reference, the following is another solution. Although C++ offers many standard-library facilities (see std::vectoror std::array) that makes programmer life easier in cases like this compared to the built-in array intrinsic low-level concepts, if you need anyway to call your printArraylike so:

我知道这篇文章的日期,但只是为了完整性和将来参考,以下是另一种解决方案。尽管 C++ 提供了许多标准库工具(请参阅std::vectorstd::array),与内置数组内在低级概念相比,在这种情况下使程序员的生活更轻松,如果您无论如何需要这样称呼您printArray

printArray(sally, 2, 3);

you may redefine the function this way:

你可以这样重新定义函数:

void printArray(int* theArray, int numberOfRows, int numberOfColumns){
    for(int x = 0; x < numberOfRows; x++){
        for(int y = 0; y < numberOfColumns; y++){
            cout << theArray[x * numberOfColumns + y] << " ";
        }
        cout << endl;
    }
}

In particular, note the first argument and the subscript operation:

特别注意第一个参数和下标操作:

  • the function takes a pointer, so you pass the name of the multidimensional array which also is the address to its first element.

  • within the subscript operation (theArray[x * numberOfColumns + y]) we access the sequential element thinking about the multidimensional array as an unique row array.

  • 该函数采用一个指针,因此您传递多维数组的名称,该名称也是其第一个元素的地址。

  • 在下标操作 ( theArray[x * numberOfColumns + y]) 中,我们访问顺序元素,将多维数组视为唯一的行数组。

回答by neuront

If you pass array as argument you must specify the size of dimensions except for the first dim. Compiler needs those to calculate the offset of each element in the array. Say you may let printArraylike

如果将数组作为参数传递,则必须指定除第一个维度外的维度大小。编译器需要那些来计算数组中每个元素的偏移量。说你可以让printArray喜欢

void printArray(int theArray[][3], int numberOfRows, int numberOfColumns){
    for(int x = 0; x < numberOfRows; x++){
        for(int y = 0; y < numberOfColumns; y++){
            cout << theArray[x][y] << " ";
        }
        cout << endl;
    }
}