c++ - 数组类型无效

标签 c++ types

我正在尝试运行下一个代码,但我在循环内收到 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/

相关文章:

java - 面向对象编程中的类型

c++ - 使用 mysql++ 构建应用程序时找不到 mysql_version.h

c# - 将 double 值转换为二进制值

c# - 如何找到最小协变类型以使两种类型之间最合适?

c++ - uintptr_t 与 DWORD_PTR 的用法

android - Java 中没有无符号类型,为什么 DEX 可执行文件中有无符号类型?

c++ - 如何配置xmlrpc-c深渊服务器主机?

c++ - 初始化临时字符串以保持条件可读是否可以接受?

c++ - 如何在 C++ 中隐藏我的 AWS S3 访问 key 和 secret ?

c# - 需要检查两个 View 模型的类型以查看它们是否源自同一基础