473,385 Members | 1,622 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,385 software developers and data experts.

Incorrect operator<< is called.

Can someone explain what's going on with the test program below ?

On Windows / Visual Studio .NET

os << x << endl; uses operator<< ( ostream & , const string & )

but

cout << x << endl; uses operator<< ( ostream & , const X & ) as I expect.

On Linux / g++, operator<< ( ostream & , const X & ) is used in both cases.

Is this a bug on Windows or is there something in the standard that I'm not
aware off ?

Test program:

#include <iostream>
#include <istream>
#include <string>
#include <sstream>

using namespace std;

class X
: public string
{ };

ostream& operator<< ( ostream& strm, const X& x )
{
strm << "Okidoki : '" << static_cast<string>( x ) << "'";
return strm;
}

int main( void )
{
X x;
ostringstream os;

os << x << endl;

cout << os.str( );

return 0;
}

Thanks.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #1
2 1365
After replying to this a minute ago and selecting "reply to all groups", I
remembered something about how crossposted messages are held up from being
posted to the non-moderated groups until the moderated ones accept it.
Since it can take a while for stuff to show up on clcm, I'll risk some ire
and post this again /just here/ so that folks might benefit from the
research I put into this. Sorry in advance for when (if?) it shows up
again...

-----------------

I've spent a while trying to pin this one down, and all I've managed to
accomplish so far is to get frustrated. First, note that the problem only
happens under VC6 and VC7.0 (under VC7.1 it works fine, as it does under
every other platform I have handy here to test on.) I've found several ways
to work around the problem, the simplest being to just cast the stream in
your insertion statement thusly:

(ostream &) os << x << endl;

Another thing you can do (in order to avoid adding the ugly casts
everywhere) would be to define another inserter that takes an ostringstream
rather than an ostream:

ostream& operator<< ( ostringstream& strm, const X& x )
{
strm << "Okidoki : '" << static_cast<string>( x ) << "'";
return strm;
}

All this leads me to suspect that either some template support deficiency
in the VC6/7.0 compiler is the culprit, or else it is something in the
libraries they use. However, the fact I get exactly the same (wrong)
behavior when using the Dinkum drop-in replacement libs with VC6 and VC7.0
makes me lean toward suggesting it is the compilers that are to blame.

HTH,
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2

I think there is a MS specific solution of the operator for the class
ostringstream in VC++ 6.

Ralf

www.oo-factory.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Jul 22 '05 #3

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

Similar topics

4
by: franky.backeljauw | last post by:
Hello, I have a problem with using a copy constructor to convert an object of a templated class to object of another templated class. Let me first include the code (my question is below): ...
2
by: Terry Santegoeds | last post by:
Can someone explain what's going on with the test program below ? On Windows / Visual Studio .NET os << x << endl; uses operator<< ( ostream & , const string & ) but cout << x << endl;...
3
by: Alicia | last post by:
Hello, I am trying to figure out how to call an overloaded operator<< inherited from a base class. #ifndef PHONECALL #define PHONECALL #include "time.h" #include "interval.h"
0
by: ma740988 | last post by:
Consider #include <iostream> #include <string> #include <map> using namespace std; struct dstream // data_stream class {
3
by: Carlo Capelli | last post by:
I found a change in the following code, that behaved correctly in VC++ 6. #include <strstream> using namespace std; void main() { char x; ostrstream(x, 100) << "pippo" << "pluto" << ends;...
4
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
1
by: buburuz | last post by:
Hi, I have a question about overloading operator<< . Actually I am trying to understand how it works when chaining multiple calls to this operator. I have a very simple class (MyOut) with an...
4
by: aaragon | last post by:
Hi everyone, I was unable to find out why my code is not compiling. I have a template class and I'm trying to write the operator<< for standard output. Does anyone know why this is not right?...
3
by: subramanian100in | last post by:
Consider the code: #include <iostream> using namespace std; int main( ) { cout << "test string "; cout.operator<<(10).operator<<(endl);
3
by: Martin T. | last post by:
Hello. I tried to overload the operator<< for implicit printing of wchar_t string on a char stream. Normally using it on a ostream will succeed as std::operator<<<std::char_traits<char> will...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.