类型无效 'int[int]' 对于数组索引

此代码发出标题中给出的编译错误,是否有人可以告诉我更改什么?


#include <iostream>

using namespace std;

int main//{

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

for /int i = 0; i &lt;= 9; ++i/{
for /int t = 0; t &lt;=9; ++t/{
for /int x = 0; x &lt;= 9; ++x/{
for /int y = 0; y &lt;= 9; ++y/{

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

}
}
}
}

for /int i = 0; i &lt;= 9; ++i/{
for /int t = 0; t &lt;=9; ++t/{
for /int x = 0; x &lt;= 9; ++x/{
for /int y = 0; y &lt;= 9; ++y/{

cout &lt;&lt; myArray[i][t][x][y] &lt;&lt; endl;

}
}
}
}

system/"pause"/;

}


先感谢您
</iostream>
已邀请:

奔跑吧少年

赞同来自:

您订阅了三维数组
myArray[10][10][10]

四次
myArray[i][t][x][y]

. 可能您必须为您的阵列添加另一个维度。 还要考虑类型容器
http://www.boost.org/doc/libs/ ... .html
, 虽然目前它可能会在你的头上。

冰洋

赞同来自:

什么改变? 除了问题 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/{

八刀丁二

赞同来自:

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


必须是


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

小明明

赞同来自:

您正在尝试使用4 de引用访问三维数组

你只需要 3 循环而不是 4, 或者
int myArray[10][10][10][10];

石油百科

赞同来自:

我认为你初始化了一个数组 3d, 但是你正在尝试访问这些数组 4 测量。

莫问

赞同来自:

只需完整的图片,此错误可能会发生在另一个情况下:当您在外部范围内声明数组时,但在“内部”字段中声明不同的变量,在可见性的内部字段中,阴影为阵列。 然后,当您尝试索引数组时,您实际上可以访问内部区域中的变量,甚至可能甚至不是数组,或者它可以是具有较少数量的测量数量的数组。

例子:


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
}

要回复问题请先登录注册