2011-03-11 42 views
0

當我int main()其唯一目的是將數據輸出到文件中下測試ofstream,那麼我可以沒有問題編譯。但是,當我在int main(...)中有其他參數時,出現以下錯誤。如何在int main(...)中聲明ofstream什麼應該是C++中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

爲什麼你'main'功能有其他參數?你包括什麼標題?你有沒有使用指令? – 2011-03-11 20:40:05

+0

的#include 的#include 的#include 的#include 的#include 「tga.h」 的#include 的#include 的#include 的#include 的#include flora 2011-03-11 21:31:17

+0

上述所有都包括。如果我沒有int main()中的double phi_fcn(),那麼會出現另一個錯誤,說phi_fcn()沒有聲明。一旦我添加它,該錯誤消失。 – flora 2011-03-11 21:31:49

回答

5

您需要#include <fstream>並且有資格ofstream作爲std::ofstream

另外請注意,您的主要特徵不是由標準不允許的,可能或可能不會導致隨機不可預測的問題給你。

7

在C++中,main必須具有以下兩個簽名中的一個:

int main(); 

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

它是非法寫一個main函數,採用這些以外的任何參數,因爲這些通常由操作系統或C++語言運行時設置。這可能是你錯誤的原因。

或者,你得到這些錯誤可能表明,你忘了#include相應的頭文件的事實。你的#include <fstream>在你的程序的頂部嗎?

+0

如果實現有一個主函數作爲第三個參數的擴展,那將是合法的。但我猜想失蹤包括更可能。 :-) – 2011-03-11 21:22:58

+0

我確實在程序的頂部有#include。如果我沒有int main()中的double phi_fcn(),那麼會出現另一個錯誤,說phi_fcn()沒有聲明。 – flora 2011-03-11 21:30:20

1

如果您收到一個錯誤,指出phi_fcn()沒有聲明,則有另一種#包括你需要添加具有在其定義的功能,大概。將它作爲參數添加到main()不是解決方案。

相關問題