Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return stringstream from a function

Clearly I am missing something important about stringstreams in general here, but could someone explain why

#include <sstream>
using namespace std;

stringstream foo() {
  stringstream ss;
  return ss;
}

Fails with

In file included from /usr/include/c++/4.4/ios:39,
             from /usr/include/c++/4.4/ostream:40,
             from /usr/include/c++/4.4/iostream:40,
             from rwalk.cpp:1:/usr/include/c++/4.4/bits/ios_base.h: In copy constructor ‘std::basic_ios<char,    std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’:/usr/include/c++/4.4/bits/ios_base.h:790: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.4/iosfwd:47: error: within this context
/usr/include/c++/4.4/iosfwd: In copy constructor ‘std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream(const std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >&)’:
/usr/include/c++/4.4/iosfwd:75: note: synthesized method ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’ first required here 
/usr/include/c++/4.4/streambuf: In copy constructor ‘std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::basic_stringbuf(const std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >&)’:
/usr/include/c++/4.4/streambuf:770: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]’ is private
/usr/include/c++/4.4/iosfwd:63: error: within this context
/usr/include/c++/4.4/iosfwd: In copy constructor ‘std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream(const std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >&)’:
/usr/include/c++/4.4/iosfwd:75: note: synthesized method ‘std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::basic_stringbuf(const std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >&)’ first required here 
rwalk.cpp: In function ‘std::stringstream foo()’:
rwalk.cpp:12: note: synthesized method ‘std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream(const std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >&)’ first required here 

How does one return a stringstream from a function properly? (edit: added the headers for a complete code snippet and fixed typo)

like image 959
Hooked Avatar asked Feb 25 '11 16:02

Hooked


People also ask

Can you return a Stringstream?

You can't return a stream from a function by value, because that implies you'd have to copy the stream. C++ streams are not copyable. Save this answer.

What does Stringstream str () do?

str(): Gets and sets the string object's content in the stream. clear(): Clears the stream. <<: Adds a new string to the StringStream object. >>: Reads the content from the StringStream object.

Where is Stringstream defined C++?

To use stringstream class in the C++ program, we have to use the header <sstream>. For Example, the code to extract an integer from the string would be: string mystr(“2019”); int myInt; stringstream (mystr)>>myInt; Here we declare a string object with value “2019” and an int object “myInt”.

What is the difference between Stringstream and string?

Very Informally: A string is a collection of characters, a stream is a tool to manipulate moving data around. A string stream is a c++ class that lets you use a string as the source and destination of data for a stream.


1 Answers

After correct the type-o in the return type (noted by Mahesh), your code will not compile in C++03 because stringstream is not copyable. However if your compiler supports C++0x, turning that on allows your code to compile because stringstream is MoveConstructible.

like image 83
Howard Hinnant Avatar answered Oct 18 '22 22:10

Howard Hinnant