简体   繁体   中英

getline in istream and getline in basic_string

string text;
getline(text.c_str(),256);

1) I am getting an error "error: no matching function for call to 'getline(const char*, int)" What's wrong in the above since text.c_str() also returns a pointer to array of characters.

If I write like this

char text[256]
cin.getline(text, 256 ,'\n'); 

it works fine. What's the difference between cin.getline and getline?

2) How come

text string;
getline(cin,text,'\n') 

accepts the whole line as the input. Where is the pointer to array of characters in this one?

text.c_str() returns a const char * . You may not use it to modify the contents of the string, in any way . It only exists so that you can pass the string data to old C API functions without having to make a copy. You are not allowed to make changes because there is no way that the string object that holds the data could possibly find out about them , and therefore this would allow you to break the string's invariants.

Furthermore, std::getline accepts completely different parameters . (You would know this if you took two seconds to type 'std::getline' into Google.) The error means exactly what it says: "no matching function for call" means "you can't call the function with these kinds of parameters", because every overload of the function accepts something different (and incompatible).

std::getline accepts these parameters:

  • A stream. You have to pass this because otherwise it doesn't know where to read from.
  • A string object to read into. NOT a char buffer.
  • Optionally, a line delimiter char (same as the stream getline member function).

There is not really any such function as "cin.getline". What you are calling is the member function "getline" of the object "cin" - a global variable that gets defined for you when you #include <iostream> . We normally refer to this according to what class the function is defined in - thus, std::istream::getline .

std::istream::getline accepts these parameters:

  • A char buffer.
  • Optionally, a line delimiter char.

It does not need a stream parameter because it is a member function of the stream: it uses whatever stream we called it with.

I don't really get what the questioner is trying to do.

C++ allows function overloading, so the compiler is looking for a free function called getline that matches the parameters you have passed, and no such function exists, nor should it exist (what would getline(const char*, int) do anyway?)

The question has been asked many times why getline(istream&, string&) is a "free" function and not part of the iostream interface. Answers suggested have been that iostream outdates STL or that iostream has no dependent on the basic_string class (anywhere, which is also why opening files is done with raw pointers), and Herb Sutter would commend making getline a free-function because he feels class member functions should be minimal (and std::string has far too many, eg the find functions which could be free ones that use the class).

One thing about that function though is how useful it is as you do not need to pre-allocate a buffer and "guess" how big to make it. (Having said that if you read from a big file into a std::string you could bad_alloc if there are no newlines to be found!).

text.c_str() is an array of const characters. The const means neither you nor any function (including any sort of getline ) may write into it.

There's no portable way to use a string as a writeable array of characters. But when necessary, you can use a vector<char> instead:

std::vector<char> buffer(256);
std::size_t len = cin.getline(&buffer[0], buffer.size());
std::string text(&buffer[0], len);

But just using the string overload of getline is probably best here.

string::c_str() returns a const char * , not a char * . getline works with a string, so what you want is

string text;
getline(cin, text, '\n');

This version of getline allows the string to grow as much as needed. If you need to limit it to a certain number of characters, you would need to use a vector<char> as in the previous answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM