[英]no matching function for call to ‘std::basic_ifstream<char>::basic_ifstream(QString&)’
这是我第一次使用 QString,我不知道如何解决这个错误:
Personanetscape.cpp:16: 错误:没有匹配的函数来调用'std::basic_ifstream::basic_ifstream(QString&)'
这是我的.h
#include <iostream>
#include <fstream>
#include <string>
#include <qstring.h>
#include <qmap.h>
typedef QMap<QString, QString> Map;
class PersonaNetscape
{
private:
std::ifstream fileIn;
Map map;
public:
PersonaNetscape(QString nameFileIn);
~PersonaNetscape();
bool personIn();
QString search(QString field);
};
这是我的 .cpp
#include "Personanetscape.h"
PersonaNetscape::PersonaNetscape(QString nameFileIn)
: fileIn(nameFileIn) //this is line 16 in my code
{
if(!fileIn)
throw nameFileIn;
}
我该如何解决?
谢谢。
使用 QFile 而不是 std::ifstream。
我更喜欢 QFile 而不是 std::ifstream 的最初理由是因为一般来说,如果我正在编写使用 QString(或另一个 Qt 框架类)的代码,并且还有其他 Qt 框架类可以实现我想要做的事情(在这种情况下 QFile),我将更喜欢它,因为它会更容易,而且我不必担心转换类型/奇怪的边缘情况。
Scheff 补充了一个很好的理由,在使用 QStrings 时更喜欢 QFile,但我完全忽略了这一点。 他说:
std::string 实际上是编码不可知的,其中 QString 提供了与各种编码相互转换的方法,并且 Qt 中字符串的内部处理是通过定义良好的编码完成的。 因此,与使用 .toStdString().c_str() 的“hack”相比,QFile 的使用将使应用程序为更好地处理编码和区域设置内容做好准备。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.