473,473 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

ofstream ambiguity problem

4 New Member
Hi! I have a complex class like

Expand|Select|Wrap|Line Numbers
  1. class Complex
  2. {
  3.     double       re;
  4.     double       im;
  5.  
  6. public:
  7.     Complex();
  8.     Complex( const double &r, const double &i );
  9.     Complex( const double &d );
  10.     friend ofstream &operator<<( ofstream &stream, const Complex &c );
  11.     [other stuff]
  12. };
  13.  
If I now write

Expand|Select|Wrap|Line Numbers
  1. ofstream ofs( "file.txt" );
  2. ofs << 1;
  3.  
this generates C2666 in MSVC - overloads have similar conversions. Looks like ofs << 1 requires an implicit conversion and the compiler is unsure whether to use basic_ostream<_Elem,_Traits>::operator <<(int) or to convert int to Complex and use operator <<(std::ofstream &,const Complex &).

I could fix the problem in one of the following ways:
1. Remove operator<< from Complex.
2. Remove constructor Complex( const double &d ) from Complex.
3. Write ofs.basic_ostream<char, char_traits<char>>::operator<<( 1 ) instead of ofs << 1.

But nothing of the above seems acceptable. I can't believe things are really this screwed up, it doesn't seem like I'm doing anything wrong. Any suggestions?
Apr 17 '08 #1
4 3003
weaknessforcats
9,208 Recognized Expert Moderator Expert
Using Visual Sutdio.NET 2008 I get:

error C2666: 'operator <<' : 17 overloads have similar conversions

The real culprit here is the symbolic constant 1. You are no supposed to have constants like this in a position where the compiler has to play multiple choice.

If this is truly an int (the default), then assign this value to an int and << the int.

I would not typecast. I don't believe in it with C++ unless you are calling a relic C function. If you aren't then I believe your design is flawed.
Apr 17 '08 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
I got to thinking about your problem and thought that maybe if your friend was:
Expand|Select|Wrap|Line Numbers
  1.   friend ostream &operator<<( ostream &stream, const Complex &c );
  2.  
it would help since an ofstream IS-A ostream. In fact, every inserter I have seen has used and ostream& argument.

I tried this using your code and got a successful compile.
Apr 17 '08 #3
rince
4 New Member
Oh, thanks, it really compiles ok with ostream. Still it's strange that it doesn't work with ofsream, why would there be any conversions? Such stuff really pisses me off.
Apr 17 '08 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
As long as you use defined types you will be OK. But if you use a symbolic constant you open yourself up to ambiguity.

That 1, for example:

Does the compiler:
1) use it as an int with operator<<(ostream&, int&)?

or,

2) does it use it as a char with operator<<(ostream&, char&)?

or,

3) does it convert it to a double and use operator<<(ostream&, double&)?

etc..
etc...

Don't give the compiler that latitude. If you have an int. Use an int variable.
Apr 18 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Chase Bradford | last post by:
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...
2
by: slyphiad | last post by:
i'm kinda new at c++ so be patient ^_^ i was just wondering if u guys could help me to solve this problem that i had. i'm trying to create 5 sequential files using ofstream. this is what i...
5
by: Squid Seven | last post by:
I'm trying to use a pointer to an ofstream object and having problems: ofstream *sessionFile = NULL; if( directory == "" ) sessionFile = new ofstream( fileName.c_str(), ios::out ); else {
2
by: bob | last post by:
Hi, I'm working with an ofstream object and using it to print the contents on an 100 * 100 matrix. i.e. lots of elements. the _matrix variable below is defined as std::vector<std::string,...
5
by: shyam | last post by:
HI I have a logging application wherin I create a ofstream object say, ofstream logger; by doing this logger.open(/* some path*/,ios_base:app);
1
by: ictheion | last post by:
Hi, I am having a problem utilizing ofstream*'s stored in a map (map<char, ofstream*>). I'm having basically the same problem with a map<char, boost::mutex*>. My code compiles, but at run-time I...
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...
5
by: Gary Wessle | last post by:
Hi I have a map<string, doublem_temperatures which gets updated often. I need to save the data to files corresponding to each string each time the map is updated, I am expecting about 80 files...
15
by: aaragon | last post by:
Hello, does anyone have a clue about this error? and how to solve it? It seems to be trivial to me, but not for the compiler. I'm using g++ 4.2 on an Ubuntu Linux system: // main() .......
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
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
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...
0
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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 ...
0
muto222
php
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.