提问人:stelios 提问时间:12/5/2018 更新时间:12/5/2018 访问量:167
G++ 坚持使用 stdio.h getline 而不是 std::getline (string)
g++ insists looking to stdio.h getline instead of std::getline (string)
问:
我的计划的一部分:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;
/* Works for istringstream */
void func(const string& text) {
istringstream ff(text);
string s;
getline(ff, s, '-');
}
/* Doesnt work for ifstream */
void read_input_file(const ifstream& inputFile) {
string s;
getline(inputFile, s, '\n');
}
int main(int argc, char * argv[]){
ifstream inputfile;
ofstream outputFile;
inputfile.open(argv[1], ifstream::in);
if (!inputfile.is_open())
return -1;
read_input_file(inputfile);
inputfile.close();
return 0;
}
istringstream 和 ifstream 都是 ,但是它试图与从 ...istream
read_input_file
getline
stdio.h
test.cpp:23:28: error: no matching function for call to ‘getline(const ifstream&, std::__cxx11::string&, char)’
getline(inputFile, s, '\n');
g++ -Wall -c "test.cpp"
test.cpp: In function ‘void read_input_file(const ifstream&)’:
test.cpp:23:28: error: no matching function for call to ‘getline(const ifstream&, std::__cxx11::string&, char)’
getline(inputFile, s, '\n');
^
In file included from /usr/include/c++/8/cstdio:42,
from /usr/include/c++/8/ext/string_conversions.h:43,
from /usr/include/c++/8/bits/basic_string.h:6400,
from /usr/include/c++/8/string:52,
from /usr/include/c++/8/bits/locale_classes.h:40,
from /usr/include/c++/8/bits/ios_base.h:41,
from /usr/include/c++/8/ios:42,
from /usr/include/c++/8/ostream:38,
from /usr/include/c++/8/iostream:39,
from test.cpp:1:
/usr/include/stdio.h:622:18: note: candidate: ‘__ssize_t getline(char**, size_t*, FILE*)’
extern __ssize_t getline (char **__restrict __lineptr,
^~~~~~~
/usr/include/stdio.h:622:18: note: no known conversion for argument 1 from ‘const ifstream’ {aka ‘const std::basic_ifstream<char>’} to ‘char**’
...
任何想法为什么匹配错误的候选人,而不是 std::getline
答:
1赞
NathanOliver
12/5/2018
#1
您的问题是在. 需要修改,因此没有需要 .不幸的是,编译器通过提供帮助并向您显示其他可能的重载,并没有让您意识到这一事实。inputFile
const
read_input_file
inputFile
getline
const ifstream&
评论