我正在尝试使用std :: getline,但是我的编译器告诉我未识别getline?
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <fstream>
#include <cstdlib>
int main(){
using namespace std;
string line;
ifstream ifile("test.in");
if(ifile.is_open()){
while(ifile.good()){
getline(ifile,line);
}
}
}
[std::getline
在std::getline
标头中定义。
string
此外,您的代码未使用#include <string>
,cstring
,cstdio
或cmath
中的任何内容;为什么要包括这些?
EDIT:为了澄清对cstdlib
和cstring
标头的混淆,string
将C运行时库的 cstring
的内容拉到string.h
命名空间中; std
是C ++标准库的一部分,包含string
,getline
(及其专业std::basic_string<>
和std::string
)等–两个very不同的头。 >
正如ildjarn所指出的,该函数在std::wstring
中声明,很遗憾您在以下位置未收到错误:
之所以这样,是因为getline来自字符串库,您需要while( getline(ifile,line) ) {
}
或#include <string>