More problems inheriting from streambuf

Discussion in 'C++' started by Christopher Benson-Manica, Feb 26, 2004.

  1. 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*

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Feb 26, 2004
    #1
    1. Advertising

  2. "Christopher Benson-Manica" <> wrote in message
    news:c1l33t$sv6$...
    > 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
    John Harrison, Feb 26, 2004
    #2
    1. Advertising

  3. 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.
    =?iso-8859-1?Q?Juli=E1n?= Albo, Feb 26, 2004
    #3
  4. John Harrison <> spoke thus:

    > 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 ;(

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Feb 26, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Viktor_Lundstr=F6m?=

    Inheriting streambuf

    =?ISO-8859-1?Q?Viktor_Lundstr=F6m?=, Sep 24, 2003, in forum: C++
    Replies:
    3
    Views:
    3,672
    Dietmar Kuehl
    Sep 25, 2003
  2. Peter Jansson
    Replies:
    1
    Views:
    498
    David Rubin
    Nov 8, 2004
  3. Michael
    Replies:
    4
    Views:
    397
    Matt Hammond
    Jun 26, 2006
  4. Christopher Pisz
    Replies:
    2
    Views:
    588
    James Kanze
    Dec 12, 2007
  5. Raymond Martineau

    Inheriting from streambuf - xsputn

    Raymond Martineau, Nov 9, 2008, in forum: C++
    Replies:
    3
    Views:
    1,084
    Raymond Martineau
    Nov 9, 2008
Loading...

Share This Page