2017-10-13 79 views
3

我想接下來要運行的代碼,我得到無效的錯誤類型對於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]); 
+1

j是一個整數。你不能用它作爲數組。你得到一個錯誤,因爲它是錯誤的。 –

+0

看起來你有兩個不同的東西,都命名爲'j' - 一個數組(其代碼中沒有顯示其聲明)和一個循環索引。給他們不同的名字。 –

+0

什麼是'j'聲明爲外環之前?該信息將解釋錯誤消息。 – Peter

回答

1

3號線我看j[i]=0;,導致我假設j高於我們在這裏看到聲明的數組。然而,你然後鬼j使用它作爲你的for循環中的int。接近底部,你做j[i] = j[i] + cos(phi[j]);。但是,在此範圍內j不是您的陣列,它是該循環的迭代編號的整數

重命名陣列或INT在for循環

0

看起來你已經宣佈J所示的數組。並在for(int j = 0; j < Particles; j ++)您聲明另一個j爲整數。 此時j變成局部整數j,所以它不識別前面的數組j。 嘗試重命名陣列別的東西或重命名爲(INT J = 0;Ĵ<顆粒; J ++)爲(INT K = 0; k <顆粒; k ++)或類似的東西