【ERROR】no matching function for call to 'std::basic_ifstream::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

 

 


					


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM