C++ Error no matching function for call to 'std::basic_ofstream<char>::basic_ofstream(std::string&)

Table of Contents

问题 

解释:

解决方法:


问题 

string filename = "1.txt";  
ifstream fin;  
fin.open(filename);  

上述语句会产生如下错误:

error: no matching function for call to 'std::basic_ifstream<char>::basic_ofstream(std::string&)

解释:

std::ofstream can only be constructed with a std::string if you have C++11 or higher. Typically that is done with -std=c++11 (gcc, clang). If you do not have access to c++11 then you can use the c_str() function of std::string to pass a const char * to the ofstream constructor.

Also as Ben has pointed out you are using an empty string for the second parameter to the constructor. The second parameter if proivided needs to be of the type ios_base::openmode.

With all this your code should be

ofstream entrada(asegurado); // C++11 or higher

or

ofstream entrada(asegurado.c_str());  // C++03 or below

原文地址:地址

解决方法:

也就是我这里使用的C++编译版本比较低,这里解决方式可以使用.c_str()方法。

string filename = "1.txt";  
ifstream fin;  
fin.open(filename.c_str());  

当然,也有第二种解决方式,比较low一点:

cout<<"输入文件名及路径以创建该文件,如:E:/a.txt"<<endl;
char fileName[10];
cin>>fileName;
ofstream fout(fileName);

 


作者:无涯明月

上篇: C++ sort函数


 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦否

文章对你有用?不妨打赏一毛两毛

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值