Deriving from std::basic_streambuf

Discussion in 'C++' started by Guest, Aug 8, 2004.

  1. Guest

    Guest Guest

    I can not seem to get this to compile with either gcc 3.4 or MSVC++ .NET
    2003... (source follows first):
    ---= BEGIN MyStreamBuffer.h =---
    // #IFDEF redundancy left out.
    #include <streambuf>

    template< class MyElem >
    class MyStreamBuffer : public std::basic_streambuf< MyElem >
    {
    protected:
    int_type overflow( int_type Data = traits_type::eof() );
    };

    --= END MyStreamBuffer.h =---

    ---= BEGIN MyStreamBuffer.cpp =---
    #include "MyStreamBuffer.h"

    template< class MyElem >
    int_type MyStreamBuffer::eek:verflow( int_type Data = traits_type::eof() )
    {
    return 0;
    }

    ---= END MyStreamBuffer.cpp =---

    And then I have a basic "int main( /* etc */ )" which merely constructs the
    object (without 'new'). I know there's something wrong... as both compilers
    I have tried fail to compile the source. I have tried (in the *.cpp file) to
    use 'int_type' as "MyStreamBuffer< MyElem >::int_type to no avail.

    *If* I leave the definition for the 'overflow' method within the definition
    of the class it works no problem (that I can see). I would be very grateful
    if someone had a reason why this can't be done... or just a very basic class
    that is derived from std::basic_streambuf that has implementation and the
    declaration separated by files.

    Thank you!
     
    Guest, Aug 8, 2004
    #1
    1. Advertising

  2. On Sun, 08 Aug 2004 06:16:38 GMT, <> wrote:

    > I can not seem to get this to compile with either gcc 3.4 or MSVC++ .NET
    > 2003... (source follows first):
    > ---= BEGIN MyStreamBuffer.h =---
    > // #IFDEF redundancy left out.
    > #include <streambuf>
    >
    > template< class MyElem >
    > class MyStreamBuffer : public std::basic_streambuf< MyElem >
    > {
    > protected:
    > int_type overflow( int_type Data = traits_type::eof() );
    > };
    >
    > --= END MyStreamBuffer.h =---
    >
    > ---= BEGIN MyStreamBuffer.cpp =---
    > #include "MyStreamBuffer.h"
    >
    > template< class MyElem >
    > int_type MyStreamBuffer::eek:verflow( int_type Data = traits_type::eof() )
    > {
    > return 0;
    > }
    >
    > ---= END MyStreamBuffer.cpp =---
    >
    > And then I have a basic "int main( /* etc */ )" which merely constructs
    > the
    > object (without 'new'). I know there's something wrong... as both
    > compilers
    > I have tried fail to compile the source. I have tried (in the *.cpp
    > file) to
    > use 'int_type' as "MyStreamBuffer< MyElem >::int_type to no avail.
    >
    > *If* I leave the definition for the 'overflow' method within the
    > definition
    > of the class it works no problem (that I can see). I would be very
    > grateful
    > if someone had a reason why this can't be done... or just a very basic
    > class
    > that is derived from std::basic_streambuf that has implementation and the
    > declaration separated by files.
    >
    > Thank you!
    >


    Two things wrong

    1) You should not repeat the default argument when you define a function.
    Drop ' = traits_type::eof()' from your function definition (but not your
    declaration).

    2) All template code should go into header files anyway (otherwise you get
    linker errors). See the FAQ
    http://www.parashift.com/c -faq-lite/containers-and-templates.html#faq-34.12

    john
     
    John Harrison, Aug 8, 2004
    #2
    1. Advertising

  3. Guest

    Guest Guest

    "John Harrison" <> wrote in message
    news:eek:psceg92pw212331@andronicus...
    > On Sun, 08 Aug 2004 06:16:38 GMT, <> wrote:
    >
    >
    > Two things wrong
    >
    > 1) You should not repeat the default argument when you define a function.
    > Drop ' = traits_type::eof()' from your function definition (but not your
    > declaration).
    >
    > 2) All template code should go into header files anyway (otherwise you get
    > linker errors). See the FAQ
    >

    http://www.parashift.com/c -faq-lite/containers-and-templates.html#faq-34.12
    >
    > john


    Thank you for the quick response. (I thought the inability to separate code
    was a problem with old compilers.) I have since been educated... thank you
    John.

    ty!
     
    Guest, Aug 8, 2004
    #3
  4. On Sun, 08 Aug 2004 06:26:01 GMT, <> wrote:

    > "John Harrison" <> wrote in message
    > news:eek:psceg92pw212331@andronicus...
    >> On Sun, 08 Aug 2004 06:16:38 GMT, <> wrote:
    >>
    >>
    >> Two things wrong
    >>
    >> 1) You should not repeat the default argument when you define a
    >> function.
    >> Drop ' = traits_type::eof()' from your function definition (but not your
    >> declaration).
    >>
    >> 2) All template code should go into header files anyway (otherwise you
    >> get
    >> linker errors). See the FAQ
    >>

    > http://www.parashift.com/c -faq-lite/containers-and-templates.html#faq-34.12
    >>
    >> john

    >
    > Thank you for the quick response. (I thought the inability to separate
    > code
    > was a problem with old compilers.) I have since been educated... thank
    > you
    > John.
    >


    The export keyword solves this problem, but very few compilers support it.
    One that does is Comeau C++.

    http://www.comeaucomputing.com/4.0/docs/userman/export.html

    john
     
    John Harrison, Aug 8, 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. Guest
    Replies:
    0
    Views:
    406
    Guest
    Aug 28, 2004
  2. Guest
    Replies:
    1
    Views:
    569
    Jonathan Turkanis
    Aug 29, 2004
  3. Steven T. Hatton

    I/O std::basic_streambuf put and get areas?

    Steven T. Hatton, Jul 3, 2005, in forum: C++
    Replies:
    3
    Views:
    531
    Steven T. Hatton
    Jul 4, 2005
  4. sri
    Replies:
    2
    Views:
    366
    Carl Barron
    Apr 6, 2007
  5. Christopher
    Replies:
    1
    Views:
    417
    Christopher Pisz
    Jan 29, 2008
Loading...

Share This Page