Solved

Why is the compiler complain in stringstream?

Posted on 2012-04-13
9
565 Views
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 =  mDataStream.read(buffer, 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 =  mDataStream.read(buffer, buffersize);

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

What I am doing wrong?
0
Comment
Question by:prain
9 Comments
 
LVL 45

Expert Comment

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

Expert Comment

by:cup
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 = mDataStream.read (buff, sizeof(buff));
   return 0;
}

Open in new window

0
 

Author Comment

by:prain
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...

mDataStrea.read(buffer, bufferSize);

But if I capture the return type, I get this problem...
std::istream* readStream =  mDataStream.read(buffer, 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.
-prain
0
 

Author Comment

by:prain
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 = sstr.read(buffer, size);


  return 0;
}
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

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

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

Accepted Solution

by:
Infinity08 earned 125 total points
ID: 37852429
>> std::istream readStream =  mDataStream.read(buffer, buffersize);

Copying streams is not defined.


>> std::istream* readStream =  mDataStream.read(buffer, buffersize);

You can't assign a reference to a pointer.


Try this :

std::istream& readStream =  mDataStream.read(buffer, buffersize);

Open in new window

0
 

Author Closing Comment

by:prain
ID: 37852447
Thanks
0
 
LVL 33

Expert Comment

by:sarabande
ID: 37854593
FYI:

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 (!mDataStream.read(buffer, 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:

(mDataStream.read(buffer, 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.

Sara
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now