std没有会员'getline'?

pig*_*d10 13 c++ io std getline

我正在尝试使用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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

ild*_*arn 28

std::getlinestring标题中定义.

#include <string>
Run Code Online (Sandbox Code Playgroud)

此外,您的代码不使用任何从cstring,cstdio,cmath,或cstdlib; 为什么要包括这些呢?

编辑:为了澄清有关cstringstring标头的混淆,cstringC运行时库的 内容拉string.hstd命名空间; stringC++标准库的一部分,包含getline,std::basic_string<>(及其特化std::stringstd::wstring)等 - 两个非常不同的标题.

  • @Pig Head:`cstring`适用于C-in-C++.它定义了`std`命名空间中所有旧的C字符串函数(`strlen`等). (3认同)
  • 那些`cwhatever`标准头几乎总是一样的:将旧的C头导入`std`命名空间.它与C++没有太大关系,因为它增加了C兼容性,减少了命名空间污染. (2认同)