Incomplete type is not allowed: stringstream
#include <sstream>
and use the fully qualified name i.e. std::stringstream ss;
how copy from one stringstream object to another in C++?
Indeed, streams are non-copyable (though they are movable).
Depending on your usage, the following works quite well:
#include <iostream>
#include <sstream>
int main()
{
std::stringstream ss1;
ss1 << "some " << 123 << " stuff" << std::flush;
std::stringstream ss2;
ss2 << ss1.rdbuf(); // copy everything inside ss1's buffer to ss2's buffer
std::cout << ss1.str() << std::endl;
std::cout << ss2.str() << std::endl;
}
Output:
some 123 stuff
some 123 stuff
Does stringstream copy the string on which it is constructed?
Yes. From http://en.cppreference.com/w/cpp/io/basic_stringstream/basic_stringstream
2) Uses a copy of
str
as initial contents of the underlying string device. The underlyingbasic_stringbuf
object is constructed asbasic_stringbuf<Char,Traits,Allocator>(str, mode)
.
Problem with ostringstream and copy constructor
Copy constructor and copy-assignment of any stream class in C++ has been made private
. That means, you cannot make copy of std::ostringstream
object:
std::ostringstream ss;
std::ostringstream ss1(ss); //not allowed - copy-constructor is private
ss1=ss; //not allowed - copy-assignment is private
stringstream::str copy lifetime
This is not a problem of stringstream
. It is the problem of c_str
function - it returns pointer to a char*
representation of std::string
, but it lives only during lifetime of an original string. When you call char *str = ss.str().c_str()
the following actually happens:
string tmp = ss.str();
char *str = tmp.c_str();
tmp.~string (); // after that line the pointer `str` is no longer valid
c_str
is a dangerous function provided only for compatibility and speed purposes and you should avoid using it.
C++: Why re-input stringstream doesn't work? the content remains the same
The last read of sso >> r1 >> r2 >> r3;
puts the stream into fail state because no more characters were available while it was still trying to read more characters for r3
.
In fail state the stream cannot be written to or read from. To fix your problem move sso.clear();
up to before sso << "D"
.
You can see different behaviour if you output an extra space after C
because then the stream does not enter fail state (it successfully reads r3
while there is still the space in the stream).
C++ incomplete type is not allowed when using stringstream
You've failed to #include <sstream>
.
This is allowed to compile (any standard header can include an arbitrary number of other standard headers), but you should normally include the headers for the "stuff" you're using (and stringstream
is declared in <sstream>
). Once you include the header, VC++ compiles it just fine.
[Thanks Remy] Since you're using std::string
, you should also #include <string>
.
non-copying istringstream
It's fairly trivial to write a basic std::streambuf
class that reads from a given memory area. You can then construct an istream
from this and read from that.
initializing a C++ std::istringstream from an in memory buffer?
Note that the lifetime of the buffer pointed to be c_str()
is very limited, though, and there's no guarantee that a call to c_str()
want cause some copying although I don't know of any implementations where it does.
Related Topics
Is It Safe to Delete a Null Pointer
Converting a Hex String to a Byte Array
How to Mix Swift With C++? Like the Objective-C .Mm Files
Rand() Returns Same Values When Called Within a Single Function
What Breaking Changes Are Introduced in C++11
Do You Use Null or 0 (Zero) For Pointers in C++
How to Erase an Element from Std::Vector≪≫ by Index
Can You Remove Elements from a Std::List While Iterating Through It
Copy a File in a Sane, Safe and Efficient Way
How to Link to a Library With Code::Blocks
Pure Virtual Function With Implementation
C++ Templates That Accept Only Certain Types
Remove Spaces from Std::String in C++
Debugging With Command-Line Parameters in Visual Studio
Difference Between Private and Protected Members of C++ Classes