2012-03-13 35 views
0
Deneme::Deneme(string FileName){ 

fstream textfile; 
textfile.open(FileName); 
} 

這給我一個錯誤,但是當我鍵入textfile.open(「randomname」);而不是textfile.open(FileName);似乎沒有問題。爲什麼是這樣?這可能是一個簡單的問題,但我是初學者,找不到解決方案。fstream的類和使用字符串作爲參數

+0

相關:http://stackoverflow.com/questions/32332/why-dont-the-stdfstream-classes-take-a-stdstring – 2012-03-13 21:57:41

回答

1

fstreams只接受const char*。改爲使用textfile.open(FileName.c_str());fstream textfile(FileName.c_str());(儘管C++ 11接受const std::string&)。 這是一個handy site來查看如何聲明構造函數和函數。

0

fstream的open方法在你傳遞一個std :: string值的時候需要一個const char指針,我想這可能是錯誤。嘗試:

textfile.open(FileName.c_str()); 
0

使用標準的C++符合系統:C++ 2011提供的構造和功能open()採取std::string const&。對於C++ 2011之前的系統,您需要使用name.c_str()傳遞給文件流。

相關問題