C++ 中 int main 中的 ofstream 的参数/声明应该是什么?
当我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 C++ 中,
main
必须具有以下两个签名之一:或者
编写采用除这些参数之外的任何参数的
main
函数是非法的,因为这些参数通常是设置的通过操作系统或 C++ 语言运行时。这可能是您的错误的原因。或者,您收到这些错误的事实可能表明您忘记
#include
适当的头文件。您是否在程序顶部#include
?In C++,
main
must have one of the following two signatures:or
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?您需要
#include
并将ofstream
限定为std::ofstream
。另请注意,标准不允许您的 main 签名,并且可能会也可能不会给您带来随机的不可预测的问题。
You need to
#include <fstream>
and qualifyofstream
asstd::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.
如果您收到错误消息,指出 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.