473,396 Members | 1,689 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.

ostream and ofstream overloading

Hey all
I have a class Foo, and I'm trying to overload the << operator for both
the ostream and ofstream for it. This way I should have two seperate
formats for output, one for files and another for the screen. Right now the
two declarations are:
std::ofstream& operator<<( std::ofstream &fos, const Foo &theClass);
std::ostream& operator<<( std::ostream &fos, const Foo &theClass);
The problem is that I keep getting this error with G++

choosing 'std::basic_ostream<_CharT, _Traits>& std::operator<<
(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT,
_Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>,
_Alloc = std::allocator<char>]' over 'std::ofstream& operator<<(
std::ofstream &fos, const Foo &theClass)'

because worst conversion for the former is better than the worst conversion
for the latter
Googling for this error didn't turn up anything productive, so I was
wondering if it is possible to even do this?
Thanks.


Jul 19 '05 #1
3 7796
"Chase Bradford" <ch******@hotmail.com> wrote in message
news:bp***********@news.fsr.net...
I have a class Foo, and I'm trying to overload the << operator for both
the ostream and ofstream for it. This way I should have two seperate
formats for output, one for files and another for the screen. Right now the two declarations are:
std::ofstream& operator<<( std::ofstream &fos, const Foo &theClass);
std::ostream& operator<<( std::ostream &fos, const Foo &theClass); I wouldn't do this, because it will not work as expected when
chaining output calls with << :
void test( std::ofstream& fos , const Foo& theClass )
{
fos << theClass; // will call the 2nd operator
fos << "The class is: " << theClass; // will call the 1st one
// because the first << operator will return an std::ostream
}
The problem is that I keep getting this error with G++

choosing 'std::basic_ostream<_CharT, _Traits>& std::operator<<
(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT,
_Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>,
_Alloc = std::allocator<char>]' over 'std::ofstream& operator<<(
std::ofstream &fos, const Foo &theClass)'

[ NB: you should provide a complete sample if possible ]
Does the Foo class happen to provide an implicit conversion,
eventually to std::string ? [now this can only be a guess]

I'm afraid you are abusing overloading and implicit conversions.
They can be useful techniques, but are also very error prone.
It would be safer to:
- Use dedicated functions instead of already overloaded operators
when wanting to provide non-standard behavior.
- Implicit conversions should be used sparingly -- named member
functions shall be preferred in most cases.
I hope this helps,
Ivan
--
http://ivan.vecerina.com
Jul 19 '05 #2
Thanks Ivan,

I was worried that might be the case so I had already implemented it with
the Push2File dedicated function. I was just hoping there was a way to do
this using the << operator with the particular streams.

"Ivan Vecerina" <pl*****************@ivan.vecerina.com> wrote in message
news:bp**********@newshispeed.ch...
"Chase Bradford" <ch******@hotmail.com> wrote in message
news:bp***********@news.fsr.net...
I have a class Foo, and I'm trying to overload the << operator for both the ostream and ofstream for it. This way I should have two seperate
formats for output, one for files and another for the screen. Right now

the
two declarations are:
std::ofstream& operator<<( std::ofstream &fos, const Foo &theClass);
std::ostream& operator<<( std::ostream &fos, const Foo &theClass);

I wouldn't do this, because it will not work as expected when
chaining output calls with << :
void test( std::ofstream& fos , const Foo& theClass )
{
fos << theClass; // will call the 2nd operator
fos << "The class is: " << theClass; // will call the 1st one
// because the first << operator will return an std::ostream
}
The problem is that I keep getting this error with G++

choosing 'std::basic_ostream<_CharT, _Traits>& std::operator<<
(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT,
_Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' over 'std::ofstream& operator<<(
std::ofstream &fos, const Foo &theClass)'

[ NB: you should provide a complete sample if possible ]
Does the Foo class happen to provide an implicit conversion,
eventually to std::string ? [now this can only be a guess]

I'm afraid you are abusing overloading and implicit conversions.
They can be useful techniques, but are also very error prone.
It would be safer to:
- Use dedicated functions instead of already overloaded operators
when wanting to provide non-standard behavior.
- Implicit conversions should be used sparingly -- named member
functions shall be preferred in most cases.
I hope this helps,
Ivan
--
http://ivan.vecerina.com

Jul 19 '05 #3
On Sun, 16 Nov 2003 13:36:15 -0800, "Chase Bradford"
<ch******@hotmail.com> wrote:
Thanks Ivan,

I was worried that might be the case so I had already implemented it with
the Push2File dedicated function. I was just hoping there was a way to do
this using the << operator with the particular streams.


You could add formatting manipulators for your class. Check out this
article:

http://www.cuj.com/documents/s=7998/cujcexp1902austern/

Tom
Jul 19 '05 #4

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

Similar topics

9
by: Ingo Nolden | last post by:
Hi there, I am writing c++ for some months now. I think I know the language now, but not yet all the tricky things of stl. For a kernel which is not using mfc I am writing a serialization. For...
13
by: Bo Peng | last post by:
Dear list, I need to add "disable output" feature to a bunch of objects that output to ostream (or ofstream). The least intrusive way to do this is pass them ofstream("/dev/null") but this makes...
6
by: pembed2003 | last post by:
Hi all, Given something like: std::ofstream out_file("path"); how do I extract the file descriptor from out_file? Is it possible? What I want is to extract the file descriptor and then pass...
2
by: waitan | last post by:
#include <algorithm> #include <iostream> #include <fstream> #include <string> #include <vector> #include <sstream> #include <iterator> #include <iomanip> using namespace std;
4
by: manontheedge | last post by:
i'm working on a C++ program that uses operator overloads (which i'm just learning to use), and i have the ostream working fine, but i'm thinking i'm using it wrong somehow because the ofstream...
7
by: laniik | last post by:
Hi, I am trying to understand some existing code involving casting std::ostream (which supposedly compiles, but doesn't seem to on my compiler) basically, i want to pass a std::ostream as a...
8
by: kevin | last post by:
Hello! So I was reading O'Reilly's C++ in a Nutshell when I came accross something interesting: The class definition for basic_ostream contains numerous overloaded operator<< functions: ...
4
by: LewGun | last post by:
Usually, when we program with C++, we like to include the header"iostream", but it's too big, so i want to use the header "ostream" (only use it to output), as follows ,there is a piece of...
3
by: Thomas Lenz | last post by:
The code below should allow to use a comma instead of << with ostreams and include a space between two operands when comma is used. e.g. cout << "hello", "world", endl; should print the line...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.