473,657 Members | 2,530 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheriting from streambuf - xsputn

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::xs putn(const char*, std::streamsize )':
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/streambuf.tcc:8 0:
error: `std::streamsiz e std::basic_stre ambuf<_CharT,
_Traits>::xsput n(const _CharT*, std::streamsize ) [with _CharT = char,
_Traits = std::char_trait s<char>]' is protected
a.cpp:45: error: within this context
Nov 9 '08 #1
3 4722
Raymond Martineau wrote:
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?
You are calling x->xsputn(), but you do not inherit from *x. Since
x->xsputn() is protected, you get an access violation.

You could call the xsputn() method of the actual base of SplitStream.

[snip]
Best

Kai-Uwe Bux
Nov 9 '08 #2
On Nov 9, 7:24*am, Raymond Martineau <bk...@ncf.cawr ote:
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);
* * * * }
};
This looks basically like a filtering streambuf, a more or less
standard idiom. (See
http://kanze.james.neuf.fr/articles/fltrsbf1.html and
http://kanze.james.neuf.fr/articles/fltrsbf2.html, for example.)
In which case, the functions you absolutely have to override are
overflow and sync. (The default implementation xsputn will call
overflow, but you may want to override it as well, for
optimization reasons.)

There's code in Boost to do most of the work for you.
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 fact that you're trying to access a protected element in a
streambuf from which you haven't derived. You can access
protected streambuf members in a SplitStream object, but not
protected members in just anything.
The streambuf is working after a few code changed. The call
from xsputn was changed to sputn, along with additional
methods overflow and sync.
So what is the problem. Overflow should forward to sputc, and
xsputn to sputn (and sync to pubsync, if it's relevant).

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 9 '08 #3
On Sun, 9 Nov 2008 04:58:35 -0800 (PST), James Kanze
<ja*********@gm ail.comwrote:
>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 fact that you're trying to access a protected element in a
streambuf from which you haven't derived. You can access
protected streambuf members in a SplitStream object, but not
protected members in just anything.
Okay, that makes sense. Apparantly, C++ isn't Java, which allows that
form of access.
Nov 9 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
12030
by: Viktor Lundström | last post by:
Hi! I was planning to wrap a socket inside an iostream, to achieve something like this: TCPSocket s(..); s << "Hello!" << endl; Information on the web seems to be a bit scarce on how to do this. I have understood that there is a relation between streambuf and iostream, where one should be able to extend streambuf and override the underflow/overflow functions (and do send()/recv() for example).
3
2179
by: Christopher Benson-Manica | last post by:
This is starting to seem ridiculous to me :( #include <streambuf> #include <iostream> class TWFileStream : public std::streambuf { private: char cbuf;
5
1940
by: Christopher Benson-Manica | last post by:
Yes, it's me again, with my dear, dear friends std::streambuf and std::ostream... On the bright side, the code compiles. On the gloomy side, I can't seem to get overflow() or xsputn() called... class TWFileBuf : public std::streambuf { private: bool Open( name ) {/* debug printf - does show up */} protected:
4
1715
by: Christopher Benson-Manica | last post by:
Is there any way to prevent the streambuf from calling overflow or xsputn unless I specifically flush the stream? -- 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.
3
2299
by: Christopher Benson-Manica | last post by:
Unless there is something wrong with the following complete C++ file, I think my quest to stream-ize our code will be at an end :( #include <sstream> #include <iostream> using namespace std; class linebuf : public streambuf {
9
2703
by: Fred Ma | last post by:
Hello, I posted previously under the thread: How to break this up into streambuf/ostream I've asked our library to get "C++ IOStreams and Locales..." by A. Langer et al. Meantime, I've looked at the C++ standard (ISO/IEC 14882) from the web to get a grip on the relationship between ostream methods and streambuf methods: flush(), operator<<(), sputc(),
4
3049
by: Thomas Matthews | last post by:
Hi, My objective is to add enable & disable functionality to the ofstream class. I want to have a log file where I can disable and enable output to it. When my first tests pass, I want to disable the annotations written to the log file, but enable the text from the newer test cases. I've searched the C++ newsgroups and the FAQ-Lite
1
2521
by: Johannes Barop | last post by:
Hello, i try to implement a streambuffer. I overwrote streambuf::overflow() and streambuf::xsputn(). Both are protected and virtual (http://www.cplusplus.com/ref/iostream/streambuf/). But somehow my OutBuf::xsputn() is not beeing used. But the orginal basic_streambuf::xsputn() calls my OutBuf::overflow(). http://pastebin.com/483016 - OutBuf.h
1
3435
by: AJG | last post by:
Hi there. I am using a library called SOCI that has a method to set a stream which it uses to log SQL queries. The signature is as follows: void setLogStream(std::ostream *s); This works great when used with something like setLogStream(&std::cerr). However, I would like to add more context to the query logged. Specifically, I'd like to add the thread ID. So, instead of the query logged looking like:
0
8413
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7352
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.