More problems inheriting from streambuf

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

This is starting to seem ridiculous to me :(

#include <streambuf>
#include <iostream>

class
TWFileStream : public std::streambuf
{
private:
char cbuf[2];

protected:
TLSFile lsf;
virtual int_type overflow (int_type c)
{
*cbuf=c;
if(!LastOpSucceeded||!(LastOpSucceeded=lsf.Write(cbuf)))
return(EOF); return(c);
}
virtual std::streamsize xsputn(const char *s, std::streamsize num)
{
lsf.Write(s);return(0);
}
};

If there's nothing wrong with the above definition, *why* am I getting
errors about "operator<< not implemented in type TWFileStream"? I
thought that was the whole grand goal of this plan to inherit from
streambuf? *sigh*
 
J

John Harrison

Christopher Benson-Manica said:
This is starting to seem ridiculous to me :(

#include <streambuf>
#include <iostream>

class
TWFileStream : public std::streambuf
{
private:
char cbuf[2];

protected:
TLSFile lsf;
virtual int_type overflow (int_type c)
{
*cbuf=c;
if(!LastOpSucceeded||!(LastOpSucceeded=lsf.Write(cbuf)))
return(EOF); return(c);
}
virtual std::streamsize xsputn(const char *s, std::streamsize num)
{
lsf.Write(s);return(0);
}
};

If there's nothing wrong with the above definition, *why* am I getting
errors about "operator<< not implemented in type TWFileStream"? I
thought that was the whole grand goal of this plan to inherit from
streambuf? *sigh*

Need to go back to the book I think.

TWFileStream is not a stream, and doesn't have operator<< defined. It's a
streambuf which must be attached to an ostream object.

TWFileStream buffer;
ostream stream(&buffer);
stream << "some string";

Normally you wrap this in a class

class TWFileStreamItIsReally : public std::eek:stream
{
public:
TWFileStreamItIsReally()
{
rdbuf(&buffer);
}
private:
TWFileStream buffer;
};

john
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Christopher Benson-Manica escribió:
If there's nothing wrong with the above definition, *why* am I getting
errors about "operator<< not implemented in type TWFileStream"? I
thought that was the whole grand goal of this plan to inherit from
streambuf? *sigh*

You need to write your own streambuf, and your own stream that uses this
streambuf. There is a detailed sample at:
http://www.informatik.uni-konstanz.de/~kuehl/iostream/

Regards.
 
C

Christopher Benson-Manica

John Harrison said:
TWFileStream is not a stream, and doesn't have operator<< defined. It's a
streambuf which must be attached to an ostream object.

Yep, that's in the book... sorry to trouble you, although on the
bright side I think this is finally starting to make sense :)
Normally you wrap this in a class
class TWFileStreamItIsReally : public std::eek:stream
{
...
};

I see, I'm in the process of doing that now, although I've made it a
template class so I can do different things with the buffer. Is that
a good idea?

*sigh* Only 13 compile errors to go, and then I get to find out that
it doesn't work anyway ;(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top