R
Raymond Martineau
I have the following code segment for a class intended to split output
between cout and a file:
class SplitStream : public std::streambuf
{
std::streambuf *x;
public:
SplitStream()
{
x = cout.rdbuf(this);
}
~SplitStream()
{
cout.rdbuf(x);
}
std::streamsize xsputn ( const char * s, std::streamsize n )
{
return x->xsputn(s, n);
}
};
When I try compiling it, I get an error message stating that
"x->xsputn(s, n)" is a call to a protected method (in spite of the
fact that it's being called from a derived class.) What is the actual
cause behind that error message?
The streambuf is working after a few code changed. The call from
xsputn was changed to sputn, along with additional methods overflow
and sync.
For those who want the error message:
a.cpp: In member function `virtual std::streamsize
SplitStream::xsputn(const char*, std::streamsize)':
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/streambuf.tcc:80:
error: `std::streamsize std::basic_streambuf<_CharT,
_Traits>::xsputn(const _CharT*, std::streamsize) [with _CharT = char,
_Traits = std::char_traits<char>]' is protected
a.cpp:45: error: within this context
between cout and a file:
class SplitStream : public std::streambuf
{
std::streambuf *x;
public:
SplitStream()
{
x = cout.rdbuf(this);
}
~SplitStream()
{
cout.rdbuf(x);
}
std::streamsize xsputn ( const char * s, std::streamsize n )
{
return x->xsputn(s, n);
}
};
When I try compiling it, I get an error message stating that
"x->xsputn(s, n)" is a call to a protected method (in spite of the
fact that it's being called from a derived class.) What is the actual
cause behind that error message?
The streambuf is working after a few code changed. The call from
xsputn was changed to sputn, along with additional methods overflow
and sync.
For those who want the error message:
a.cpp: In member function `virtual std::streamsize
SplitStream::xsputn(const char*, std::streamsize)':
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/streambuf.tcc:80:
error: `std::streamsize std::basic_streambuf<_CharT,
_Traits>::xsputn(const _CharT*, std::streamsize) [with _CharT = char,
_Traits = std::char_traits<char>]' is protected
a.cpp:45: error: within this context