1

C++ での不明なエラー、エラー: ';' の前に一次式が必要です トークン。私が C++ で書いたコードは次のとおりです。

 #include <iostream>
 #include <math.h>
 #include <stdio.h>
 #define G 6.674E-11
 using namespace std;

 int main()
 {
 //Ms = Mass of sun, Me = Mass of Earth, Fg = Gravitational force between them, As =                                   Acceleration of Sun, Ae = Acceleration of Earth, Ve_x
 // = initial velocity of Earth in x direction, Ve_y = initial velocity of Earth in y          direction, Vs_x = initial velocity of the Sun in x direction
 // Vs_y = initial velocity of sun in y direction, t = time, F = Gravitational force   `    between the two bodies.

    float Ms, Me, Fg, As, Ae, Ve_x, Ve_y, Vs_x, Vs_y, pos_E, pos_S, r_x, r_y, r, t;
    float S_dist;
    float E_dist;
    float F;
    float Ve[2];
    float Vs[2];
    float pe[2];
    float ps[2];


  FILE *fileptr;

    cout <<"Enter mass of the Sun in kg\n";
    cin >> Ms;
    cout <<"Enter mass of the earth in kg\n";
    cin >> Me;
    cout <<"Enter intial velocity of the sun in x direction in m/s\n";
    cin >> Vs[0];
    cout <<"Enter intial velocity of the sun in y direction in m/s\n";
    cin >> Vs[1];
    cout <<"Enter intial velocity of the earth in x direction in m/s\n";
    cin >> Ve[0];
    cout <<"Enter intial velocity of the earth in y direction in m/s\n";
    cin >> Ve[1];
    cout <<"Enter intial position of the sun in x component\n";
    cin >> ps[0];
    cout <<"Enter intial position of the sun in y direction\n";
    cin >> ps[1];
    cout <<"Enter intial position of the earth in x direction\n";
    cin >> pe[0];
    cout <<"Enter intial position of the earth in y direction\n";
    cin >> pe[1];


  for (t=0; t<30000; t+1)
 {
  float E_dist;
  float S_dist;
  float F;

    E_dist=sqrt( ((pe[0]-pe[0])*(pe[0]-pe[0])) + ((pe[1]-pe[1])*(pe[1]-pe[1])) );
    S_dist=sqrt( ((ps[0]-ps[0])*(ps[0]-ps[0])) + ((ps[1]-ps[1])*(ps[1]-ps[1])) );

    r_x=( (pe[0]-pe[0]) - (ps[0]-ps[0]) );
    r_y=( (pe[1]-pe[1]) - (ps[1]-ps[1]) );
    r= sqrt( (r_x)*(r_x) + (r_y)*(r_y) );

    F=(G*Me*Ms)/(r*r);

    Ae = F/Me;
    As = F/Ms;

    Ve_x = Ve[0];
    Ve_y = Ve[1];
    Vs_x = Vs[0];
    Vs_y = Vs[1];
    }
    cout<<"At time "<<t<<":\n The position of the Sun is "<<S_dist<<"\n The position of   the Earth is "<<E_dist
    <<"\n The acceleration of the Sun is "<<As<<" \n The acceleration of the Earth is "<<Ae<<" \nThe velocity of the Sun in the x direction is "
    <<Vs_x<<" \n The velocity of the Sun in the y direction is "<<Vs_y<<" \n The velocity of the Earth in the x direction is "<<Ve_x<<
    " \n The velocity of the Earth in the y direction is "<<Ve_y<<" \n The gravitational force between the Sun and the Earth is "<<F<<; // ERROR OCCURRED HERE.

} 

どんな助けにも感謝します、ありがとう。

4

3 に答える 3

8

エラーは、最後の行が次のように終わることだと思います。

<<F<<;

<<演算子が 1 つの引数だけに適用されていることに注意してください。こんなこと書くつもりだったの?

<<F<<endl;

わかりやすくするために、その出力行を複数の行に分割することを強くお勧めします。あなたが今持っているものは正しいですが、読むのは信じられないほど難しいです. のように書き直します。

cout << "At time " <<t<<":\n The position of the Sun is "<<S_dist<<"\n";
     << " The position of   the Earth is "<<E_dist << "\n";
     << "The acceleration of the Sun is "<<As<<"\n"
     << "The acceleration of the Earth is "<<Ae<<"\n";
     << "The velocity of the Sun in the x direction is "<<Vs_x<<" \n";
     << "The velocity of the Sun in the y direction is "<<Vs_y<<" \n";
     << "The velocity of the Earth in the x direction is "<<Ve_x<< "\n";
     << "The velocity of the Earth in the y direction is "<<Ve_y<<" \n";
     << "The gravitational force between the Sun and the Earth is "<<F<<;

行番号情報がより役立つため、このエラーを見つけやすくなります。<<また、読みやすくするために、演算子の間にスペースを追加することをお勧めします。

于 2012-04-05T20:17:43.140 に答える
5

templatetypedefはコンパイラ エラーを指摘しましたが、コードには別の問題があります:forループは無限です:

for (t=0; t<30000; t+1)

次のようにする必要があります。

for (t=0; t<30000; t++)

またはフロートの悪いスタイルでインクリメント(演算子++)を使用しtいますか?:float

for (t = 0; t < 30000; t+=1.0f)
于 2012-04-05T20:19:03.887 に答える
0

最初の構文エラーは<<、ステートメントの最後に別のものがあるためです。印刷する別の var があるか、 endline のように使用したい場合cout<<F<<;を除き、 に変更してください。cout<<F;cout<<F<<endl;

もう 1 つの構文エラーもあります。あなたmainはintを返しますが、最後に何も返しませんでした:

return 0;

もう 1 つの非常に重要な問題は、変数に関するものですF

論理エラー!!!

int main()
{
....
  float S_dist; //real S_dist !!!
  float E_dist; //real E_dist !!!
  float F;      //real F !!!
  for (t=0; t<30000; t++)
  {

     float F; // just exists in for!!!
     float E_dist; //just exists in for!!!
     float E_dist; //just exists in for!!!
  ....
     F=(G*Me*Ms)/(r*r); //changing local F, which just exists in for
     E_dist=sqrt( ((pe[0]-pe[0])*(pe[0]-pe[0])) + ((pe[1]-pe[1])*(pe[1]-pe[1])) );
     S_dist=sqrt( ((ps[0]-ps[0])*(ps[0]-ps[0])) + ((ps[1]-ps[1])*(ps[1]-ps[1])) );
  }
  cout<<F<<E_dist<<S_dist; //this prints the main vars!
  return 0;
} 

ローカル変数についてもっと読んでください。t+1まったく変わらないことに注意してtください!代わりにステートメントの最後の部分にt++orを書く必要があります! 無限ループに陥らない限り、まったく成長しません!t+=1fort

于 2012-04-05T20:53:00.370 に答える