繁体   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