flora
flora

Reputation: 3

What should be the arguments/declaration for ofstream in int main in C++?

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;

}

Upvotes: 0

Views: 2944

Answers (3)

Mark B
Mark B

Reputation: 96281

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.

Upvotes: 5

Tyrel
Tyrel

Reputation: 727

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.

Upvotes: 1

templatetypedef
templatetypedef

Reputation: 372982

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?

Upvotes: 7

Related Questions