std has no member 'getline'?

pighead10 picture pighead10 · Apr 25, 2011 · Viewed 30.1k times · Source

I'm trying to use std::getline, but my compiler is telling me that getline isn't identified?

#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);
        }
    }
}

Answer

ildjarn picture ildjarn · Apr 25, 2011

std::getline is defined in the string header.

#include <string>

Also, your code isn't using anything from cstring, cstdio, cmath, or cstdlib; why bother including these?

EDIT: To clarify the confusion regarding the cstring and string headers, cstring pulls the contents of the C runtime library's string.h into the std namespace; string is part of the C++ standard library and contains getline, std::basic_string<> (and its specializations std::string and std::wstring), etc. -- two very different headers.