gpt4 book ai didi

c++ - 没有匹配的函数来调用 ‘std::basic_ifstream::basic_ifstream(QString&)’

转载 作者:行者123 更新时间:2023-12-02 10:04:24 25 4
gpt4 key购买 nike

这是我第一次使用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添加了一个很好的理由,在使用我完全忽略的QString时更喜欢QFile。他说:

std::string is actually encoding agnostic where QString provides methods to convert to and from various encodings, and the internal handling of strings in Qt is done with a well-defined encoding. Hence, the usage of QFile will prepare the application for better handling of encoding and locale stuff than "the hack" with .toStdString().c_str().

关于c++ - 没有匹配的函数来调用 ‘std::basic_ifstream<char>::basic_ifstream(QString&)’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60984371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com