【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