2017-08-04 82 views
-5

我正在與一些功能的程序:ofstream引用使用字符串?

int totalDays(ofstream &outputFile, int noEmployee) 
{ 
string fileName = "employeeAbsences.txt"; 

outputFile.open(fileName); 

不過,我不知道該怎麼稱呼它:

int main() 
{ 

int employeesNumber = employees(); 
string fileName = "employeeAbsences.txt"; 

employees(); 

totalDays(fileName, employeesNumber); 

的文件名是在呼叫強調說,它不能成爲一個串。我應該叫什麼名字的第一個功能?

+3

當函數使用'std :: ofstream'時,你試圖傳遞'std :: string'。你期望會發生什麼? – DimChtz

+0

請問如何調用函數,請幫助我嗎? – deadth

+1

您還需要傳遞文件名。 –

回答

2

讓我們申報(並定義)正確的函數:

void totalDays(std::ofstream& outputFile, 
       int employee_quantity, 
       const std::string& filename) 
{ 
    outputFile.open(filename.c_str()); 
    //... 
} 

我們稱之爲:

std::ofstream outputFile; 
totalDays(outputFile, employeesNumber, fileName); 

totalDays功能所需要的輸出文件(流)回傳給main和它需要文件的名稱才能打開文件。所以,你需要將這些項目傳遞給你的函數。

+0

非常感謝! – deadth

2

您的totalDays()函數的第一個參數是對某個ofstream對象的引用,但傳入的是字符串(文件名)。這不會像你報告一樣編譯。

在main()中,您應該實例化一個ofstream對象並將此對象作爲第一個參數傳遞給totalDays()函數。

+0

謝謝指點! – deadth