C++ Getline issues (No instance of overloaded function "getline"

owenbradstreet

I know I know. This question has been asked before, but I've looked at all the answers and none seem to solve my problem. When I use the getline function to get the contents of a line in the file, it doesn't work.

getline(file, line);

'File' is declared here:

ifstream File;
File.open("fruit.txt");

and 'line' is declared here:

int line = 0;

Getline is underlined in red with this message:

getline
no instance of overloaded function "getline" matches the argument list
argument types are :(std::ifstream, int)

What this means is no instance of getline has the argument list of the file stream and an integer.

This makes no sense as all the other questions on this matter state exactly that, that the arguments are the file stream and an integer.

What am I doing wrong?

EDIT:

Here is the full code:

ifstream fruitFile;
fruitFile.open("fruit.txt");
int line = 0;
int C_FRUIT = getline(fruitFile, line);
fruitFile.close();

The first line should be a number, and I need it.

Christophe

getline() will read one line of text. It can't read directly an int. This is why you get your error message.

You have to be aware that there are two getline(). There is one which is istream::getline() and std::getline(). Both have different signatures. The first is a member function of a stream and is defined in the stream header; the latter is defined in the <string> header.

But pay attention: the return value of std::getline() is not an int ! It's a stream reference. This is why you get a second compiler error.

Finally if you want to read an integer x, it's easier to use extractors:

int value; 
fruitFile >> value; 
fruitFile.ignore(SIZE_MAX, '\n');   // in case you'd need to go to next line

Or if you really want to read an int in a full line:

string line;
getline(fruitFile, line); 
stringstream sst(line);     // creates a string stream: a stream that takes line as input
sst >> value;      

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

No instance of overloaded function for getline

Error No instance of overloaded function "getline" matches the argument list

How to fix "no instance of overloaded function "getline" matches the argument list" when using getline with an int

c++ getline() error, no instance of overload function "getline" matches the argument list

no matching function for getline c++

Writing getline function in c language

getline() not taking delimiter argument (ERROR: no instane of overloaded function "getline" matches argument list

issues with getline() and input stream

Unexpected behavior of linux specific getline() function in C

How to loop the getline function in C++

Multiple delimiters for getline function, c++

if statement use inside getline() function in C

Why am I having trouble with getline()? "No instance of overloaded functions matches the argument list" and "data is ambiguous"

having issues with getline(cin) to array

mixing cin and getline input issues

no matching function to call for "getline"

Validating input and getline() function

buffer cleaning in getline function

Teletype IO getLine function

Segmentation error with getline() function?

File handling in getline function

NOTE:(getline was not the issue) C++ getline() stops working in user defined function but works in main function

no instance of "getline" matches the argument list

C++ Using Getline

C++ getline and append

c++ getline and ignore

Using getline() in C++

Using GetLine with ifstream - no instance of "getline" matches the argument list

Segmentation fault in C++ program using getline and string function