c++ 编译报错——std::basic_ifstream::open

4 篇文章 0 订阅

c++ 编译报错——error: no matching function for call to ‘std::basic_ifstream::open(std::__cxx11::string&, const openmode&)’

错误出现

之前在Mac上开发正常,然后换到ubutun上的时候就开始报错。具体代码如下

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

#define FILE "stuInfo.txt"

void func(string path)
{
    ifstream ifs;
    ifs.open(path, ios::in);
    int id, pwd;
    string name;
    while(ifs >> id && ifs >> name && ifs >>pwd)
    {
        cout << id << name << pwd << endl;
    }
    ifs.close();
}

int main()
{
    string path = FILE;
    func(path);
    return 0;
}

***********************运行结果*****************************
wtzhu@ubuntu:~/MyFile/computerRoomOrderSystem$ g++ test.cpp 
test.cpp: In function ‘void func(std::__cxx11::string)’:
test.cpp:11:27: error: no matching function for call to ‘std::basic_ifstream<char>::open(std::__cxx11::string&, const openmode&)’
     ifs.open(path, ios::in);
                           ^
In file included from test.cpp:3:0:
/usr/include/c++/5/fstream:595:7: note: candidate: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]
       open(const char* __s, ios_base::openmode __mode = ios_base::in)
       ^
/usr/include/c++/5/fstream:595:7: note:   no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const char*’

仔细看了一下提示语句error: no matching function for call to ‘std::basic_ifstream<char>::open(std::__cxx11::string&, const openmode&)’ ifs.open(path, ios::in);,提示显示是函数没有匹配成功,而且open函数的第一个参数用的是std::cxx11::string类型,然后看到最后一句note: no known conversion for argument 1 from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const char*’就可以知道是这个string类型的原因导致的。

目前c++11标准开始普及,大家都开始默认支持或者使用c++11,例如GCC 5就开始默认启用C++11特性。但是由于c++11相对于c++03,很多实现的数据结构都发生了改变,所以两者并不能完全混用。
默认情况下,GCC 5在编译时会将std::string类型按c++11下std::__cxx11::basic_string 来处理,这时如果你调用的库在编译时未启用c++11特性则其中的std::string实际上是std::basic_string ,这时如果将c++11下的string当作参数传入非c++11的库时,就会出现error: cannot convert ‘const std::__cxx11::basic_string’ to ‘const char*’,或者未定义的方法引用(undefined reference)。
————————————————
参考:https://blog.csdn.net/ufolr/article/details/52669333

解决方法

  1. 使用g++11来编译
    sudo vim ~/.bashrc
    ​ 在some more ls aliases注释块的地方添加下面这两行:

    alias g++11='g++ -g -Wall -std=c++11'
    
    wtzhu@ubuntu:~/MyFile/computerRoomOrderSystem$ g++11 test.cpp 
    wtzhu@ubuntu:~/MyFile/computerRoomOrderSystem$ ./a.out 
    131141143fred131141143
    131141101tom131141101
    
  2. 将文件中string改为const char *类型

    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    
    #define FILE "stuInfo.txt"
    
    void func(const char * path)
    {
        ifstream ifs;
        ifs.open(path, ios::in);
        int id, pwd;
        string name;
        while(ifs >> id && ifs >> name && ifs >>pwd)
        {
            cout << id << name << pwd << endl;
        }
        ifs.close();
    }
    
    int main()
    {
    	const char * path = FILE;
        func(path);
        return 0;
    }
    
    ***********************运行结果*****************************
    wtzhu@ubuntu:~/MyFile/computerRoomOrderSystem$ g++ test.cpp 
    wtzhu@ubuntu:~/MyFile/computerRoomOrderSystem$ ./a.out 
    131141143fred131141143
    131141101tom131141101
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值