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
解决方法
-
使用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
-
将文件中
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