簡體   English   中英

fstream在Qt Creator中不起作用

[英]fstream does not function in Qt Creator

我是Qt新手。 我的項目編碼為Visual C ++ 2010 Express Edition,可以很好地工作。 然后,我要使用已經在Qt控制台應用程序中創建的.cpp文件(類似於VC ++ 2010中的項目)。 在“ ofstream”和“ ifstream”的編譯失敗,並顯示以下錯誤:

"no matching function for call to 'std::basic_ofstream<char>::basic_ofstream(std::basic_strinstream<char>::_string_type)'"
"no matching function for call to 'std::basic_ifstream<char>::basic_ofstream(std::string&)'"

我已經將fstream包含到cpp文件中,如下所示:

#include <QCoreApplication>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

失敗的代碼如下:

stringstream s1; s1 << "Estimated_NN_ThrMWh.csv";
ofstream outF1(s1.str());

順便說一句,我正在使用“ MinGW 4.9.1 32bit”作為我的編譯器。 有什么問題,我該如何解決? 謝謝你的幫助。

您正在將std::string傳遞給std::ofstream構造函數。 這是C ++ 11的功能 ,要使用此功能 ,您需要將-std=c++11傳遞給GCC或Clang。 MSVC會自動為其編譯器發行版所編譯的混合非C ++ 11或任何其他語言進行編譯。

如果您使用的是Qt 5的qmake,則只需執行CONFIG+=c++11 ,就可以了。 否則,您將需要類似

*gcc*:QMAKE_CXXFLAGS += -std=c++11

如果您不想依賴C ++ 11,請不要使用它,而只需執行以下操作:

std::string filename("Estimated_NN_ThrMWh.csv");
std::ofstream outF1(filename.c_str());

這將始終有效。 請注意,我刪除了stringstream,因為至少在您顯示的短代碼中,它是多余的。

您應該始終編寫如下:

string s = s1.str();
ofstream outF1(s);

而且,在我看來,您的代碼在MSVC下工作應被視為MSVC的錯誤(允許將非常量引用綁定到臨時對象)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM