C++中int main中ofstream的参数/声明应该是什么?

0 c++ ofstream

当我测试ofstreamint main(),其唯一目的是将数据输出到一个文件,然后我可以没有问题编译.但是,当我内部有其他参数时int main(...),会出现以下错误.如何申报ofstreamint 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;

}
Run Code Online (Sandbox Code Playgroud)

tem*_*def 7

在C++中,main必须具有以下两个签名之一:

int main();
Run Code Online (Sandbox Code Playgroud)

要么

int main(int argc, char* argv[]);
Run Code Online (Sandbox Code Playgroud)

编写main除此之外的任何参数的函数是非法的,因为这些参数通常由操作系统或C++语言运行库设置.这可能是您的错误的原因.

或者,您收到这些错误的事实可能表明您忘记#include了相应的头文件.你#include <fstream>是程序的顶端吗?


Mar*_*k B 5

你需要#include <fstream>和资格ofstreamstd::ofstream.

另请注意,标准不允许您签署主要签名,可能会或可能不会导致随机不可预测的问题.