【ERROR】no matching function for call to 'std::basic_ifstream<char>::basic_ifstream

错误记录:QT中使用

no matching function for call to 'std::basic_ifstream<char>::basic_ifstream(QString&, const openmode&)'
ifstream i_f_stream(fileName,ifstream::binary);
^

没有匹配对。

 

看别人的:error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)

原因是C++的string类无法作为open的参数。

 

同样,可以发现是fileName的类型不对,没有匹配上。

 QString fileName;
 ifstream i_f_stream(fileName,ifstream::binary);

#include <fstream>
using std::ifstream;
ifstream i_f_stream(fileName,ifstream::binary);

 

 

需要转换QString类型为const char*

见:【QT】QString类型转换为const char* - ostartech - 博客园 https://www.cnblogs.com/wxl845235800/p/10796840.html

    QString str =fileName;
    char *s; QByteArray//QString转换为char*
    ba = str.toLatin1();
    s = ba.data();

    using std::ifstream;
    ifstream i_f_stream(s,ifstream::binary);

 

 

【参考】

error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&) - 铁树银花 - 博客园 https://www.cnblogs.com/cszlg/archive/2012/12/15/2910424.html

c++输入文件流ifstream用法详解 - ims的博客 - CSDN博客 https://blog.csdn.net/sinat_36219858/article/details/80369255

 

 


posted @ 2019-04-29 21:36  ostartech  阅读(5770)  评论(0)    收藏  举报