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

本文详细解析了在QT环境中使用ifstream时遇到的错误:没有匹配的函数调用。问题源于QString类型的变量直接用于ifstream构造函数,导致类型不匹配。文章提供了将QString转换为const char*的方法,以正确打开文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误记录: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

 

 


转载于:https://www.cnblogs.com/wxl845235800/p/10793052.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值