How to add content to an object of type stringstream?

I am using a stringstream object as follows:

#include<iostream>
#include <istream>

using namespace std;
int main()
{
    sting str="my.string";

    std::stringstream message("HI this is firet line from initialization");

    message << " second line " << " Continueing second line";

    message << ", Add values: ";

    message << str ;


    std::cout<<"message value is :"<<message.str()<<std::endl;

    return 0;
}

with the code above, it gives an error as follows:

 error: variable 'std::stringstream message' has initializer but incomplete type

The error above is if I added the #include header file. but when i typed the message value. it becomes incomplete. that is, the value that I received from the message looks like this:

message value: second line. Continuation of the second line, Add values: my.string

any suggestion on why the first line is deleted on the output? thanks in advance

+5
source share
2 answers

stringstream defined in another header:

#include <sstream>

Also, if you want the initial content to be inserted, you need something like:

std::stringstream message("HI this is firet line from initialization",
                                             ios_base::app | ios_base::out);

app , . . .

+12

( GCC 4.7.0, mingw Windows 7: x86_64-w64-mingw32)

std::stringstream ss("initial string", ios_base::app | ios_base::out);
ss << " appended string";

ss.good() false, , ss.str() , , "initial string appended string". , , http://www.cplusplus.com/reference/sstream/stringstream/stringstream/:

ios_base:: openmode (, ios_base:: app), , stringstream, .

"" ios_base:: , , , ss.good() true:

std::stringstream ss("initial string", ios_base::ate | ios_base::in | ios_base::out);

[P.S. , ; operator <<. , , , , , .]

+6

All Articles