数组下标的无效类型'int [int]'

use*_*061 7 c++ arrays compiler-errors

这段代码会引发标题中给出的编译错误,有人能告诉我要改变什么吗?

#include <iostream>

using namespace std;

int main(){

    int myArray[10][10][10];

    for (int i = 0; i <= 9; ++i){
        for (int t = 0; t <=9; ++t){            
            for (int x = 0; x <= 9; ++x){
                for (int y = 0; y <= 9; ++y){

                myArray[i][t][x][y] = i+t+x+y; //This will give each element a value

                      }
                      }
                      }
                      }

    for (int i = 0; i <= 9; ++i){
        for (int t = 0; t <=9; ++t){
            for (int x = 0; x <= 9; ++x){
                for (int y = 0; y <= 9; ++y){

                cout << myArray[i][t][x][y] << endl;

                    }
                    }
                    }                
                    }

    system("pause");

}
Run Code Online (Sandbox Code Playgroud)

提前致谢

cop*_*pro 14

您正在myArray[10][10][10]四次订阅三维数组myArray[i][t][x][y].您可能需要为数组添加另一个维度.还可以考虑像Boost.MultiArray这样的容器,尽管这可能已经超出了你的想法.


jmu*_*llo 5

有什么改变?除了3维或4维数组问题,你应该摆脱神奇的数字(10和9).

const int DIM_SIZE = 10;
int myArray[DIM_SIZE][DIM_SIZE][DIM_SIZE];

for (int i = 0; i < DIM_SIZE; ++i){
    for (int t = 0; t < DIM_SIZE; ++t){            
        for (int x = 0; x < DIM_SIZE; ++x){
Run Code Online (Sandbox Code Playgroud)


小智 5

为了完整起见,此错误也可能在不同的情况下发生:当您在外部作用域中声明一个数组,但在内部作用域中声明另一个具有相同名称的变量时,会隐藏该数组。然后,当您尝试对数组进行索引时,您实际上正在访问内部作用域中的变量,该变量甚至可能不是数组,或者可能是维度较少的数组。

例子:

int a[10];  // a global scope

void f(int a)   // a declared in local scope, overshadows a in global scope
{
  printf("%d", a[0]);  // you trying to access the array a, but actually addressing local argument a
}
Run Code Online (Sandbox Code Playgroud)