Deriving from std::basic_streambuf

G

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!
 
J

John Harrison

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
 
G

Guest

John Harrison said:
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!
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top