0

当我测试其唯一目的是将数据输出到文件ofstreamint 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必须具有以下两个签名之一:

int main();

或者

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

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

或者,您收到这些错误的事实可能表明您忘记#include了适当的头文件。你#include <fstream>在你的程序的顶部吗?

于 2011-03-11T20:42:42.990 回答
5

您需要#include <fstream>并有资格ofstream成为std::ofstream.

另请注意,标准不允许您对 main 的签名,并且可能会或可能不会给您带来随机的不可预测的问题。

于 2011-03-11T20:44:05.247 回答
1

如果您收到一个错误,指出 phi_fcn() 未声明,那么您可能需要添加另一个 #include,其中定义了该函数。将其作为参数添加到 main() 不是解决方案。

于 2011-03-11T23:53:47.430 回答