0

データをファイルに出力することを唯一の目的としてテストofstreamしたところ、問題なくコンパイルできます。int main()ただし、中に他の引数がint main(...)あると、次のエラーが発生します。ofstreamで宣言するにはどうすればよいint main(...)ですか?

error: ‘ofstream’ was not declared in this scope
error: expected ‘;’ before ‘phi_file’
error: ‘phi_file’ was not declared in this scope

int main(int argc, char** args, double phi_fcn())
{ 

  int frame ; 

  double *x, *y, *vx, *vy ;

  x = new double[N_POINTS] ; y = new double[N_POINTS] ; 
  vx = new double[N_POINTS] ; vy = new double[N_POINTS] ; 

  char file_name[255] ;

  printf("The number of particles is N_POINTS=%d;\n",N_POINTS) ;
  printf("the box size is L=%4.2f; ",L) ;
  printf("the interaction radius is a=%17.16f;\n",a) ;
  printf("the radius of repulsion is R_R=%17.16f;\n",R_R) ;
  printf("the radius of repulsion squared is R_R_SQUARED=%17.16f;\n",R_R_SQUARED) ;
  printf("the radius of orientation is R_O=%17.16f;\n",R_O) ;
  printf("the radius of orientation squared is R_O_SQUARED=%17.16f;\n",R_O_SQUARED) ;

  // generate initial distribution of particles

  icond_uniform(x,y,vx,vy,N_POINTS) ;

  // draw the first picture

  sprintf( file_name, "tga_files/out%04d.tga", 0 );

  drawPicture(file_name,RES_X,RES_Y,x,y,N_POINTS);

ofstream phi_file;//create a phi_file to write to
phi_file.open("phi_per_timestep.dat");***

  // time stepping loop

  for (frame=1; frame<N_FRAMES; frame++) 
    {

      interact_all(x,y,vx,vy,N_POINTS);

      advect(x,y,vx,vy,N_POINTS);

      // output data into graphics file

      sprintf( file_name, "tga_files/out%04d.tga", frame );

      drawPicture(file_name,RES_X,RES_Y,x,y,N_POINTS);

      phi_file << phi_fcn();

    }
phi_file.close();
  return 0;

}
4

3 に答える 3

7

C ++では、main次の2つの署名のいずれかが必要です。

int main();

また

int main(int argc, char* argv[]);

mainこれら以外のパラメーターを受け取る関数を作成することは違法です。これらは通常、オペレーティングシステムまたはC++言語ランタイムのいずれかによって設定されるためです。これがエラーの原因である可能性があります。

#includeまたは、これらのエラーが発生しているという事実は、適切なヘッダーファイルを忘れていることを示している可能性があります。あなた#include <fstream>はあなたのプログラムのトップにいましたか?

于 2011-03-11T20:42:42.990 に答える
5

として#include <fstream>資格を得る必要があります。ofstreamstd::ofstream

また、mainの署名は標準で許可されておらず、ランダムな予測できない問題を引き起こす場合と引き起こさない場合があることにも注意してください。

于 2011-03-11T20:44:05.247 に答える
1

phi_fcn()が宣言されていないというエラーが発生した場合は、おそらくその関数が定義されている、追加する必要のある別の#includeがあります。main()にパラメータとして追加することは解決策ではありません。

于 2011-03-11T23:53:47.430 に答える