Getline issue with input of open file stream

2019-08-03 13:51发布

问题:

I'm trying to figure out why this is broke now, because I had it working and I'm not sure what is wrong. I'm trying a simple getline from a file that's been opened, however, the compiler keeps giving me errors. I've tried finding someone else with these issues, but I haven't been able to find anyone else with this. Any advice?

void Foo::bar(ifstream &inputFile)
{
// Read in the data, parse it out, and 
// call loadQueue
string input;
do {    
    getline(inputFile, input);
    loadQueue(input);
}while (!(inputFile.eof()));

}

Here is what I get in return:

g++    -c -o Airworthy.o Airworthy.cpp
Foo.cpp: In member function ‘void Airworthy::readData(std::ifstream&)’:
Foo.cpp:25:27: error: no matching function for call to ‘getline(std::ifstream&, std::string&)’
Foo.cpp:25:27: note: candidates are:
In file included from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/string:55:0,
             from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/locale_classes.h:42,
             from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/ios_base.h:43,
             from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ios:43,
             from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/ostream:40,
             from /usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/iostream:40,

Any ideas on what the issue is?

回答1:

Most likely you forgot to #include all the necessary standard headers. One possibility is:

#include <fstream>

Or perhaps you forgot to:

#include <string> 

You always have to #include all pertinent standard headers explicitly, without relying on indirect inclusion through some other headers.



回答2:

As Andy says, you need the appropriate includes. There are, however, at least two other major problems with your code (one of which will affect which includes you need):

  • You should never (or almost never) pass ifstream as an argument to a function. Unless the function is going to do an open or a close, you should pass it std::istream&, so that it can be called with any istream, and not just ifstream.

    Once you've changed this, you need to include <istream>, and not <fstream>. (<fstream> includes <istream>. And a lot more which you don't need.)

  • You should never loop on ! inputFile.eof(). It doesn't work. In your case, the loop should be

    while ( std::getline( inputFile, input ) ) {4
        //  ...
    }
    

    It works, and almost nothing else does.

    In general, a do...while loop is almost always wrong when doing input; it results in your processing the input even when it fails (which you do—any use of input after the getline but before testing whether the getline has succeeded is an error). And the results of inputFile.eof() are not really well defined until input has failed. Using istream::eof() to control a loop is almost always an error.