473,396 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Question on deriving from ostream

Hi,

I am really puzzled why my compiler does not accept the following code:-

--- CODE STARTS HERE ---
#include <iostream>
using namespace std;

class Foo
{
};

class Writer: public ostream
{
public:
Writer() : ostream(cout.rdbuf()) {}
Writer& operator<<(Foo&) {cout << "Hi"; return *this;}
};

int main(int argc, const char* argv[])
{
Foo f;
Writer w;

w << "Test"; // OK
w << f; // OK
w << '\t'; // OK
w << endl; // Not OK: Compiler complains about <unknown type>
return 0;
}
--- CODE STOPS HERE ---

Any hints? Many thanks in advance!
Hans
Jul 22 '05 #1
3 1985

"Hans De Winter" <ha**********@tiscalinet.be> wrote in message
news:87**************************@posting.google.c om...
Hi,

I am really puzzled why my compiler does not accept the following code:-

--- CODE STARTS HERE ---
#include <iostream>
using namespace std;

class Foo
{
};

class Writer: public ostream
{
public:
Writer() : ostream(cout.rdbuf()) {}
Writer& operator<<(Foo&) {cout << "Hi"; return *this;}
};

int main(int argc, const char* argv[])
{
Foo f;
Writer w;

w << "Test"; // OK
w << f; // OK
w << '\t'; // OK
w << endl; // Not OK: Compiler complains about <unknown type>
return 0;
}
--- CODE STOPS HERE ---

Any hints? Many thanks in advance!
Hans


The operator<< that you want to be called is a member of ostream. Since you
have defined your own operator<< in a derived class, that operator hides the
operator in the base class. Try removing operator<< from Writer and the
fourth line will compile (but the second won't). The other operator<< that
you are successfully calling (for "Test" and '\t') are free functions, not
member functions, so aren't hidden by the operator<< in Writer.

Moral, don't derive from ostream, there are almost certainly better ways of
doing what you are trying to do, like deriving from streambuf.

john
Jul 22 '05 #2

"John Harrison" <jo*************@hotmail.com> wrote in message
news:c1*************@ID-196037.news.uni-berlin.de...

"Hans De Winter" <ha**********@tiscalinet.be> wrote in message
news:87**************************@posting.google.c om...
Hi,

I am really puzzled why my compiler does not accept the following code:-

--- CODE STARTS HERE ---
#include <iostream>
using namespace std;

class Foo
{
};

class Writer: public ostream
{
public:
Writer() : ostream(cout.rdbuf()) {}
Writer& operator<<(Foo&) {cout << "Hi"; return *this;}
};

int main(int argc, const char* argv[])
{
Foo f;
Writer w;

w << "Test"; // OK
w << f; // OK
w << '\t'; // OK
w << endl; // Not OK: Compiler complains about <unknown type>
return 0;
}
--- CODE STOPS HERE ---

Any hints? Many thanks in advance!
Hans
The operator<< that you want to be called is a member of ostream. Since

you have defined your own operator<< in a derived class, that operator hides the operator in the base class. Try removing operator<< from Writer and the
fourth line will compile (but the second won't). The other operator<< that
you are successfully calling (for "Test" and '\t') are free functions, not
member functions, so aren't hidden by the operator<< in Writer.
better still just bring them into scope with a using declaration

Moral, don't derive from ostream, there are almost certainly better ways of doing what you are trying to do, like deriving from streambuf.

john

Jul 22 '05 #3
Hello,

There are 3 ways to fix this
(1) You can do

endl(Writer);

instead of 'Writer << endl;'. This will compile & work.

(2) If you want that 'Writer << endl' work, add this inside class Writer:

Writer& operator<<(ostream&(*f)(ostream&)) { f(*this); return *this; }

With this definition, 'Writer << endl' will compile (see Stroustrup, 21.4.6)

(3) Don't derive from ostream. In order to define 'operator<<' for Foo,
you don't need to derive from ostream.

Jacob

ha**********@tiscalinet.be (Hans De Winter) wrote:
I am really puzzled why my compiler does not accept the following code:-

--- CODE STARTS HERE ---
#include <iostream>
using namespace std;

class Foo
{
};

class Writer: public ostream
{
public:
Writer() : ostream(cout.rdbuf()) {}
Writer& operator<<(Foo&) {cout << "Hi"; return *this;}
};

int main(int argc, const char* argv[])
{
Foo f;
Writer w;

w << "Test"; // OK
w << f; // OK
w << '\t'; // OK
w << endl; // Not OK: Compiler complains about <unknown type>
return 0;
}
--- CODE STOPS HERE ---

Jul 22 '05 #4

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

Similar topics

0
by: anonymous | last post by:
I cannot compile LiDIA mathematical library version 2.1pre5 with the gmp 4.1.2 multiprecision library because the gcc compiler ver 3.2.2 20030222 Red Hat Linux 9 complains about some structures in...
1
by: hitech_guy | last post by:
Hi, I am creating my own class derving from ostream. I am facing problem in compiling with GNU G++ compiler under linux class IWCoreEXPORT IWAFTraceLine : public ostream { public:...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
20
by: Daniel | last post by:
I have the following three classes class A { public: virtual void f() = 0; }; class B: public A {
4
by: fabricemarchant | last post by:
Hi ! I've translated Andru Luvisi small LISP interpreter sl3.c http://www.sonoma.edu/users/l/luvisi/sl3.c in C++ in order to understand how it works and to experiment with small LISP....
1
by: Christopher Pisz | last post by:
I set out to make a custom logger. Examining some other code laying around, I came across one that derived from ostream, and had a associated class derived from streambuf. Is this practice a good...
0
by: James Kanze | last post by:
On 11 avr, 17:44, "mc" <mc_r...@yahoo.comwrote: OK. If the actual format is well documented, that's half the battle won already. Note, however, that reading a float as an int is still very...
3
by: DerrickH | last post by:
I have a template class with three policies: PolicyA, PolicyB, and PolicyC. The design dilemma I am having is that B and C depend upon A, but I would like my Host class to derive from all three. In...
6
by: Dan Smithers | last post by:
I want to write my own class derived from the ostream class. I have been getting errors with my templates: First, I get an error writing a nested template. If I leave the function definition...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.