473,546 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

compilation error using ofstream

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()
....
std::ofstream fout;
fout.open("hell o.out");
fout<<setfill('-')<<setw(20)<<"-"<<setfill( ' ')<<endl;
fout<<"hello "; // this is line 139
....

main.cxx:89: instantiated from here
main.cxx:139: error: ISO C++ says that these are ambiguous, even
though the worst conversion for the first is better than the worst
conversion for the second:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/
ostream.tcc:735 : note: candidate 1: std::basic_ostr eam<char, _Traits>&
std::operator<< (std::basic_ost ream<char, _Traits>&, const char*) [with
_Traits = std::char_trait s<char>]
Domain.h:507: note: candidate 2: o& operator<<(o&, const Domain&)
[with o = std::ofstream]
make: *** [main.o] Error 1

Thank you all!



Sep 26 '07 #1
15 5978
aaragon wrote:
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()
...
std::ofstream fout;
fout.open("hell o.out");
fout<<setfill('-')<<setw(20)<<"-"<<setfill( ' ')<<endl;
fout<<"hello "; // this is line 139
...

main.cxx:89: instantiated from here
main.cxx:139: error: ISO C++ says that these are ambiguous, even
though the worst conversion for the first is better than the worst
conversion for the second:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/
ostream.tcc:735 : note: candidate 1: std::basic_ostr eam<char, _Traits>&
std::operator<< (std::basic_ost ream<char, _Traits>&, const char*) [with
_Traits = std::char_trait s<char>]
Domain.h:507: note: candidate 2: o& operator<<(o&, const Domain&)
[with o = std::ofstream]
make: *** [main.o] Error 1
I don't see any "Domain.h" or types called 'Domain' in your code.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 26 '07 #2
On Sep 26, 2:41 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
aaragon wrote:
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()
...
std::ofstream fout;
fout.open("hell o.out");
fout<<setfill('-')<<setw(20)<<"-"<<setfill( ' ')<<endl;
fout<<"hello "; // this is line 139
...
main.cxx:89: instantiated from here
main.cxx:139: error: ISO C++ says that these are ambiguous, even
though the worst conversion for the first is better than the worst
conversion for the second:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/
ostream.tcc:735 : note: candidate 1: std::basic_ostr eam<char, _Traits>&
std::operator<< (std::basic_ost ream<char, _Traits>&, const char*) [with
_Traits = std::char_trait s<char>]
Domain.h:507: note: candidate 2: o& operator<<(o&, const Domain&)
[with o = std::ofstream]
make: *** [main.o] Error 1

I don't see any "Domain.h" or types called 'Domain' in your code.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Well, I overloaded operator<< to produce the output of a class that I
designed. But shouldn't the compiler deduct this from the type being
passed to operator<<???? Why is it the call ambiguous????

Sep 26 '07 #3
aaragon wrote:
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()
...
std::ofstream fout;
fout.open("hell o.out");
fout<<setfill('-')<<setw(20)<<"-"<<setfill( ' ')<<endl;
fout<<"hello "; // this is line 139
...

main.cxx:89: instantiated from here
main.cxx:139: error: ISO C++ says that these are ambiguous, even
though the worst conversion for the first is better than the worst
conversion for the second:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/
ostream.tcc:735 : note: candidate 1: std::basic_ostr eam<char, _Traits>&
std::operator<< (std::basic_ost ream<char, _Traits>&, const char*) [with
_Traits = std::char_trait s<char>]
Domain.h:507: note: candidate 2: o& operator<<(o&, const Domain&)
[with o = std::ofstream]
make: *** [main.o] Error 1

Thank you all!


a minimal version of your program compiles and creates a file contianing
'hello ' after running:

#include <fstream>

int main()
{
std::ofstream fout;
fout.open("hell o.out");
fout<<"hello ";
fout.close();
return 0;
}

did you include the first line in your code?

Sep 26 '07 #4
aaragon wrote:
On Sep 26, 2:41 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>aaragon wrote:
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()
...
std::ofstream fout;
fout.open("hell o.out");
fout<<setfill('-')<<setw(20)<<"-"<<setfill( ' ')<<endl;
fout<<"hello "; // this is line 139
...
main.cxx:89: instantiated from here
main.cxx:139: error: ISO C++ says that these are ambiguous, even
though the worst conversion for the first is better than the worst
conversion for the second:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/
ostream.tcc:735 : note: candidate 1: std::basic_ostr eam<char, _Traits>&
std::operator<< (std::basic_ost ream<char, _Traits>&, const char*) [with
_Traits = std::char_trait s<char>]
Domain.h:507: note: candidate 2: o& operator<<(o&, const Domain&)
[with o = std::ofstream]
make: *** [main.o] Error 1

I don't see any "Domain.h" or types called 'Domain' in your code.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Well, I overloaded operator<< to produce the output of a class that I
designed. But shouldn't the compiler deduct this from the type being
passed to operator<<???? Why is it the call ambiguous????
How are we supposed to know? Please, post a _complete_ (but minimal) program
that exhibits the problem (see the FAQ on how-to-post).
Best

Kai-Uwe Bux
Sep 26 '07 #5
"aaragon" <al************ **@gmail.comwro te in message
news:11******** **************@ 22g2000hsm.goog legroups.com...
On Sep 26, 2:41 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>aaragon wrote:
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()
...
std::ofstream fout;
fout.open("hell o.out");
fout<<setfill('-')<<setw(20)<<"-"<<setfill( ' ')<<endl;
fout<<"hello "; // this is line 139
...
main.cxx:89: instantiated from here
main.cxx:139: error: ISO C++ says that these are ambiguous, even
though the worst conversion for the first is better than the worst
conversion for the second:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/
ostream.tcc:735 : note: candidate 1: std::basic_ostr eam<char, _Traits>&
std::operator<< (std::basic_ost ream<char, _Traits>&, const char*) [with
_Traits = std::char_trait s<char>]
Domain.h:507: note: candidate 2: o& operator<<(o&, const Domain&)
[with o = std::ofstream]
make: *** [main.o] Error 1

I don't see any "Domain.h" or types called 'Domain' in your code.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Well, I overloaded operator<< to produce the output of a class that I
designed. But shouldn't the compiler deduct this from the type being
passed to operator<<???? Why is it the call ambiguous????
Does Domain perhaps accept a char* as a constructor?
Sep 26 '07 #6
On Sep 26, 3:14 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
"aaragon" <alejandro.ara. ..@gmail.comwro te in message

news:11******** **************@ 22g2000hsm.goog legroups.com...
On Sep 26, 2:41 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
aaragon wrote:
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()
...
std::ofstream fout;
fout.open("hell o.out");
fout<<setfill('-')<<setw(20)<<"-"<<setfill( ' ')<<endl;
fout<<"hello "; // this is line 139
...
main.cxx:89: instantiated from here
main.cxx:139: error: ISO C++ says that these are ambiguous, even
though the worst conversion for the first is better than the worst
conversion for the second:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/
ostream.tcc:735 : note: candidate 1: std::basic_ostr eam<char, _Traits>&
std::operator<< (std::basic_ost ream<char, _Traits>&, const char*) [with
_Traits = std::char_trait s<char>]
Domain.h:507: note: candidate 2: o& operator<<(o&, const Domain&)
[with o = std::ofstream]
make: *** [main.o] Error 1
I don't see any "Domain.h" or types called 'Domain' in your code.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Well, I overloaded operator<< to produce the output of a class that I
designed. But shouldn't the compiler deduct this from the type being
passed to operator<<???? Why is it the call ambiguous????

Does Domain perhaps accept a char* as a constructor?
Yes it does!!! The domain class can be created with a char* that I
pass (the name of a file). Why is this happening then?

Sep 26 '07 #7
"aaragon" <al************ **@gmail.comwro te in message
news:11******** *************@2 2g2000hsm.googl egroups.com...
On Sep 26, 3:14 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
>"aaragon" <alejandro.ara. ..@gmail.comwro te in message

news:11******* *************** @22g2000hsm.goo glegroups.com.. .
On Sep 26, 2:41 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
aaragon wrote:
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()
...
std::ofstream fout;
fout.open("hell o.out");
fout<<setfill('-')<<setw(20)<<"-"<<setfill( ' ')<<endl;
fout<<"hello "; // this is line 139
...
main.cxx:89: instantiated from here
main.cxx:139: error: ISO C++ says that these are ambiguous, even
though the worst conversion for the first is better than the worst
conversion for the second:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/
ostream.tcc:735 : note: candidate 1: std::basic_ostr eam<char,
_Traits>&
std::operator<< (std::basic_ost ream<char, _Traits>&, const char*)
[with
_Traits = std::char_trait s<char>]
Domain.h:507: note: candidate 2: o& operator<<(o&, const Domain&)
[with o = std::ofstream]
make: *** [main.o] Error 1
>I don't see any "Domain.h" or types called 'Domain' in your code.
>V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Well, I overloaded operator<< to produce the output of a class that I
designed. But shouldn't the compiler deduct this from the type being
passed to operator<<???? Why is it the call ambiguous????

Does Domain perhaps accept a char* as a constructor?

Yes it does!!! The domain class can be created with a char* that I
pass (the name of a file). Why is this happening then?
I'm not actually positive, because I can't reproduce it in test, but what it
seems is that the compiler sees you are sending a char* to an ofstream. It
looks at sees that you have a Domain constructor that accepts a char* and
has an output to an ofstream. I wouldn't think that the compiler would pick
that, but it depends on how Domain is set up. Can you post the appropriate
Domain class? At least anything having to do with the constructor and any
operator<< overrides and such.
Sep 27 '07 #8
On Sep 26, 9:36 pm, aaragon <alejandro.ara. ..@gmail.comwro te:
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()
...
std::ofstream fout;
fout.open("hell o.out");
fout<<setfill('-')<<setw(20)<<"-"<<setfill( ' ')<<endl;
fout<<"hello "; // this is line 139
...
main.cxx:89: instantiated from here
main.cxx:139: error: ISO C++ says that these are ambiguous, even
though the worst conversion for the first is better than the worst
conversion for the second:
/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/
ostream.tcc:735 : note: candidate 1: std::basic_ostr eam<char, _Traits>&
std::operator<< (std::basic_ost ream<char, _Traits>&, const char*) [with
_Traits = std::char_trait s<char>]
Domain.h:507: note: candidate 2: o& operator<<(o&, const Domain&)
[with o = std::ofstream]
make: *** [main.o] Error 1
Apparently, you have defined function operator<<( ofstream&,
....), and Domain has a constructor which can be called with a
char const*. So the first argument of this function is an exact
match, and thus a better than the operator<<( ostream&, char
const*). The second argument, of course, is a better match for
the first function, so the call is ambiguous.

The obvious question is: why on earth would you define an
operator<< which takes an ofstream, rather than an ostream? If
you define the operator to take an ostream, then both functions
are equally good matches for the first argument, and the fact
that the char const* is a better match for the second means that
it will be chosen.

--
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

Sep 27 '07 #9
aaragon <al************ **@gmail.comwro te in
news:11******** *************@2 2g2000hsm.googl egroups.com:
>>
Does Domain perhaps accept a char* as a constructor?

Yes it does!!! The domain class can be created with a char* that I
pass (the name of a file). Why is this happening then?

A literal string like "Hello" is actually of type char const [6] and
requires an implicit cast to become a char const * for operator<<.
Unless you have your domain class constructor marked as explicit, the compiler
sees that as an additional way to convert your literal string into something
that operator<< knows how to handle. In other words, both

fout << static_cast<cha r *>("Hello");

and

fout << static_cast<Dom ain>("Hello");

are viable choices and the compiler doesn't know which one you want. Of course,
you would never write like the above, but I was trying to represent it in a way
that would show that to the compiler, both are equally viable, though to your
eyes and mine, the first is the obvious choice. To tell the compiler that the
first is the obvious choice, you should mark the constructor for the Domain as explicit.

Interestingly enough VC8 doesn't see an ambiguity, though it should.

joe
Sep 27 '07 #10

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

Similar topics

2
4088
by: Laxman | last post by:
==C.H <== #include <fstream.h> class C { public: C(int i); friend ofstream& operator<<(ofstream& os, const C&); }; ------------------------------------------------------------ ==B.H <==
6
1884
by: muser | last post by:
In the following function there is an access violation error, some memory can't be read. A week ago this code did compile. Can anyone possibly tell me why my compiler is unable to read part of this function. bool processdrecord( ofstream& prnfile, ofstream& validdata, char* record ) { drecord Newdrecord; char customercode;
11
4171
by: muser | last post by:
In the code I supplied before this one, the cause of the problem is an access violation error. When I run the debugger it skips into what I can only assume is the compilers version of my code. And I don't know where the problem lies in the code. Can anyone run it through there own compiler please and tell me where and what the problem is. ...
3
9082
by: jois.de.vivre | last post by:
Hi, I'm trying to write to an ofstream, and for some reason it fails. I know I can check it with fail() or bad(), but it gives me no useful information as to why it fails. Are there any C++ functions that do this or is there any way I can check manually? Thanks, Prashant
3
3716
by: John R. Delaney | last post by:
If I try to declare an object of type fstream (or ofstream), I run into a compilation error refering to an __something function or variable. Please note, I am including <fstream>, not <fstream.h>. I do know that much. Thanks in advance for any assistance. John
2
2740
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include <string>
2
2084
by: merrittr | last post by:
I have a small program to read data from some data files , in it I use string types to build the data file name but when I try to compile it I get: (see code below error) root@rob-laptop:/home/rob/pankaj# gcc ptcllocate.cpp /tmp/ccg9j6Mi.o: In function `main':ptcllocate.cpp:(.text+0x99): undefined reference to...
4
12855
by: marathoner | last post by:
I tried your advice, and replaced "ifstream" with "std::ifstream". I also replaced instances of "ofstream" with "std::ofstream". Those syntax errors were resolved. When I added "std" to the following statement: m_InFile.open(m_sFileName, ios::in | ios::binary); which became m_InFile.open(m_sFileName, std::ios::in | std::ios::binary); I...
15
2453
by: rEvolution27 | last post by:
I'm a c++ newbie here, trying out some stuff and when I try to compile this: void create() { char name; cout << "Creating a new timetable /n Please type a name for this timetable"; cin >name; ofstream editFile; editFile.open (name, ios::out | ios::app);
0
7504
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...
0
7694
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. ...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6026
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...
0
5080
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...
0
3491
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...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
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
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.