我正在尝试运行下一个代码,但我在循环内收到 j 的无效类型错误:
for(int i = 0; i < N; i++) //steps
{
j[i]=0;
for (int j = 0; j < Particles; j++) //Particles
{
u = randnum(0,1);
dr = pow( pow( a, 1-alph ) + u * (1-alph)/B, 1/(1-alph));
phi[j] = randnum(0,M_PIl);
pre_x = x [j];
pre_y = y [j];
x[j] = pre_x + cos(phi[j]) * dr;
y[j] = pre_y + sin(phi[j]) * dr;
while( (sin(A * x[j]) + Delta * sin(C * x[j])/2) * h + H < y[j])
{
u = randnum(0,1);
dr = pow( pow( a, 1-alph ) + u * (1-alph)/B, 1/(1-alph));
phi[j] = randnum(0,M_PIl);
x[j] = pre_x + cos(phi[j]) * dr;
y[j] = pre_y + sin(phi[j]) * dr;
}
j[i] = j[i] + cos(phi[j]);
}
myfile<<j[i]<<endl; //Outputs array to txtFile
}
myfile.close();
我已经检查了其他标题相似的问题,但标题与我的案例不符。错误的确切陈述是:
invalid types ‘int[int]’ for array subscript
j[i] = j[i] + cos(phi[j]);
最佳答案
第 3 行我看到 j[i]=0;
这让我假设 j
是在我们在这里看到的上方声明的数组。但是,您随后将 j
用作 for 循环中的 int。然后在底部执行 j[i] = j[i] + cos(phi[j]);
。但是,在此范围内 j
不是您的数组,它是该循环的迭代次数的整数。
重命名数组或 for 循环中的 int
关于c++ - 数组类型无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46722617/