Why is the compiler complain in stringstream?

Posted on 2012-04-13
Last Modified: 2012-04-17
the read() function in the stringstreram class returns a istream& type. So if I do a call like this...

std::istream readStream =, buffersize);

I am getting a compilation error "synthesized method ‘std::basic_istream<char, std::char_traits<char> >::basic_istream(const std::basic_istream<char, std::char_traits<char> >&)"

Then I change to like this.

std::istream* readStream =, buffersize);

Then I get this error....
invalid conversion from ‘void*’ to ‘std::istream*

What I am doing wrong?
Question by:prain
LVL 45

Expert Comment

ID: 37845535
what language and development tool are you using?
LVL 11

Expert Comment

ID: 37846027
Take this program, change it to the way you're using it and post it back.  It is difficult to visualize what your problem is.  This one works in Visual studio

#include <sstream>

struct Dummy
   std::istringstream mResult;
   std::istream* read (char* buffer, int buffersize)
      return &mResult;
} mDataStream;

int main ()
   char buff[1024];
   std::istream* readStream = (buff, sizeof(buff));
   return 0;

Open in new window


Author Comment

ID: 37851304
I am developing on Unix/g++. I am confused with what the compiler says.
If I use the function without the return type, it's great.
For example..., bufferSize);

But if I capture the return type, I get this problem...
std::istream* readStream =, buffersize);

error: invalid conversion from ‘void*’ to ‘std::istream*’

BTW, my mDataStream is a C++ stringstream instance. So unfortunately I cannot use your code in my C++ class. I have to stick to the stringstream instance.
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.


Author Comment

ID: 37851766
Someone try this simple example and tell me why the compiler is barking..
BTW, I using Unix/g++

#include <string>
#include <iostream>
#include <sstream>

int main ()
   std::stringstream sstr;

   sstr << "Hello" << " World" ;

   char buffer[100];
   int size = 11;

   std::istream* aIStream =, size);

  return 0;

Author Comment

ID: 37851837
BTW, since stringstream inherits istream, the read() in istream is defined as this:

istream& read ( char* s, streamsize n );
LVL 53

Accepted Solution

Infinity08 earned 125 total points
ID: 37852429
>> std::istream readStream =, buffersize);

Copying streams is not defined.

>> std::istream* readStream =, buffersize);

You can't assign a reference to a pointer.

Try this :

std::istream& readStream =, buffersize);

Open in new window


Author Closing Comment

ID: 37852447
LVL 33

Expert Comment

ID: 37854593

it is intended by design that a stream cannot be copied. they explicitly didn't provide a copy constructor (and no assignment operator) cause a stream is not a normal container but has status and position members stored for streaming purposes. in your case the read member function returns a reference to istream while the stream you used was a stringstream. so you don't get a reference to the original stream but only to the istream baseclass.

the reasons why the read member function returns a reference to istream is not mainly to assign the return value to a second variable (you still could/should use the first variable) but for two other purposes. one is that the read would return NULL in case the read operation failed (error or end-of-stream), what makes it possible to do a read like

if (!, buffersize))

Open in new window

or to use a read in a while statement.

the other is that you could use the returned stream reference to pass it to next stream operation:

(, 4)).read(&buffer[4], 2);

Open in new window

what in case of read looks strange but was often used for operator>> like in

if (mDataStream >> i >> j)

Open in new window

to do a formatted read into more variables with one statement.


Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSH commands for Nas4free 21 423
Image not there 4 53
Header Errors LNK2019, LNK1120 - Unresolved Externals 4 136
Not seen Link button 5 39
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question