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

发布于 2024-10-22 02:34:52 字数 1759 浏览 1 评论 0原文

当我在 int main() 下测试 ofstream 时,其唯一目的是将数据输出到文件中,然后我可以毫无问题地编译。但是,当我在 int main(...) 中有其他参数时,会出现以下错误。如何在 int main(...) 中声明 ofstream

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;

}

When I tested ofstream under int main() whose the sole purpose is to output data into a file, then I can compile with no problem. However, when I have other arguments inside int main(...), the following errors come up. How do I declare ofstream in 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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无远思近则忧 2024-10-29 02:34:52

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

int main();

或者

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

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

或者,您收到这些错误的事实可能表明您忘记#include适当的头文件。您是否在程序顶部#include

In C++, main must have one of the following two signatures:

int main();

or

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

It is illegal to write a main function that takes any parameters other than these, since these are typically set up either by the operating system or by the C++ language runtime. This may be the cause of your error.

Alternatively, the fact that you're getting these errors could indicate that you've forgotten to #include the appropriate header files. Did you #include <fstream> at the top of your program?

小傻瓜 2024-10-29 02:34:52

您需要#include 并将ofstream 限定为std::ofstream

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

You need to #include <fstream> and qualify ofstream as std::ofstream.

Also note that your signature for main is not allowed by the standard and may or may not cause random unpredictable problems for you.

爱的那么颓废 2024-10-29 02:34:52

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

If you're getting an error saying that phi_fcn() is not declared, then there is another #include you need to be adding that has that function defined in it, probably. Adding it as a parameter to main() is not a solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文