stringstream::getline()

Feb 3, 2017 at 7:53pm
This page shows that stringstream has a getline() method:
http://www.cplusplus.com/reference/sstream/stringstream/

But when I try using it, this compile error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
rdbuf3.cpp: In function ‘int main()’:
rdbuf3.cpp:26:23: error: no matching function for call to ‘std::__cxx11::basic_stringstream<char>::getline(char* [128], int)’
   ss.getline(line, 128);
                       ^
In file included from /usr/include/c++/6.2.1/iostream:40:0,
                 from rdbuf3.cpp:16:
/usr/include/c++/6.2.1/istream:647:5: note: candidate: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::getline(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize, std::basic_istream<_CharT, _Traits>::char_type) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::char_type = char; std::streamsize = long int]
     basic_istream<char>::
     ^~~~~~~~~~~~~~~~~~~
/usr/include/c++/6.2.1/istream:647:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/6.2.1/istream:427:7: note: candidate: std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::getline(std::basic_istream<_CharT, _Traits>::char_type*, std::streamsize) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>; std::basic_istream<_CharT, _Traits>::char_type = char; std::streamsize = long int]
       getline(char_type* __s, streamsize __n)
       ^~~~~~~
/usr/include/c++/6.2.1/istream:427:7: note:   no known conversion for argument 1 from ‘char* [128]’ to ‘std::basic_istream<char>::char_type* {aka char*}’


source code:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <sstream>

int main()
{
  std::stringstream ss;
  char* line[128];

  ss << "line1\nline2\n";

  ss.getline(line, 128);  //error: no matching function for call to ‘std::__cxx11::basic_stringstream<char>::getline(char* [128], int)’
  std::cout << "first line = " << line << "\n";
}


How to fix the compile error?

Thank you.
Last edited on Feb 3, 2017 at 7:54pm
Feb 3, 2017 at 8:00pm
line is an array of pointers-to-char and getline doesn't know what to do with an array of pointers-to-char. It should probably be an array of char.
Feb 3, 2017 at 8:07pm
That fixed it!

Thanks cire.
Topic archived. No new replies allowed.