==C.H <==
#include <fstream.h>
class C
{
public:
C(int i);
friend ofstream& operator<<(ofstream& os, const C&);
};
------------------------------------------------------------
==B.H <==
#include <fstream.h>
#include "C.H"
class B
{
public:
friend ofstream& operator<<(ofstream& os, const B&);
};
------------------------------------------------------------
==B.C <==
#include "B.H"
ofstream& operator<<(ofstream& os, const B& b)
{
int i = 1;
os << i;
return os;
}
------------------------------------------------------------
CC -c B.C
"B.C", line 6: Error: Overloading ambiguity between
"std::basic_ostream<char, std::char_traits<char>>::operator<<(int)"
and "operator<<(std::basic_ofstream<char, std::char_traits<char>>&,
const C&)"
To make it easier I have trimmed down and kept only the necessary
code
to create the error.
BTW this works fine with g++ but fails with Sun Forte Compiler.
Any idea what the problem is ? 2 4073
"MiniDisc_2k2" <Ma******@nospam.com> wrote in message news:<FqEQa.5399$zd4.3627@lakeread02>... "Laxman" <pv****@yahoo.com> wrote in message news:d8**************************@posting.google.c om... ==C.H <== #include <fstream.h> class C { public: C(int i); friend ofstream& operator<<(ofstream& os, const C&); }; ------------------------------------------------------------ ==B.H <== #include <fstream.h> #include "C.H"
class B { public: friend ofstream& operator<<(ofstream& os, const B&); }; ------------------------------------------------------------ ==B.C <== #include "B.H"
ofstream& operator<<(ofstream& os, const B& b) { int i = 1; os << i; return os; } ------------------------------------------------------------ CC -c B.C "B.C", line 6: Error: Overloading ambiguity between "std::basic_ostream<char, std::char_traits<char>>::operator<<(int)" and "operator<<(std::basic_ofstream<char, std::char_traits<char>>&, const C&)"
To make it easier I have trimmed down and kept only the necessary code to create the error. BTW this works fine with g++ but fails with Sun Forte Compiler. Any idea what the problem is ?
This may be unstandardized, I'm not sure, but I can tell you what the problem is. Your class C has a constructor which takes a parameter of an integer. Thus, i can be converted to a C if necessary. There is automatically an operator << for ofstream which takes an int, but you're also defining one which takes a C. As an int can be readily converted to a C, the compiler gets confused as to which one you want. A C cannot be converted to an int, so perhaps the solution is to cast it to a C. Also, perhaps explicitly typecasting it to an int would resolve the ambiguity to the compiler (even though it already is an int).
Thanks for replying. I did try to cast it explicitly to int but with
no luck!!
In the above example, I am trying to print a random datatype (int) but
my intent is to print a data member of B which is actually another
class, say A and C takes that same A as constructor's parameter.
"Steve Pinard" <sN************@zoominternet.net> wrote in message news:<3f**********@corp.newsgroups.com>... class C { C(int i) ... };
As posted, declaring the constructor this way allows implicit conversion of an int to a C. This is usually NOT what you want the compiler to do for you, and is the source of some hard-to-track-down problems (case in point?).
For constructors with only one parameter, I almost always use "explicit" to prevent the compiler from doing that:
class C { explicit C(int i) ... };
Have to credit Scott Meyers for this. I learned it from one of his Effective C++ books.
-- Steve Pinard
"Dr. Spock is the only Star Wars character I know" - My Mom
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =----- http://www.newsfeeds.com - The #1 Newsgroup Service in the World! -----== Over 80,000 Newsgroups - 16 Different Servers! =-----
Thanks Steve. It works!! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Edward Diener |
last post by:
Is there a way to override the default processing of the assignment operator
for one's own __value types ? I realize I can program my own Assign method,
and provide that for end-users of my class,...
|
by: xuatla |
last post by:
Hi,
I encountered the following compile error of c++ and hope to get your help.
test2.cpp: In member function `CTest CTest::operator+=(CTest&)':
test2.cpp:79: error: no match for 'operator='...
|
by: user |
last post by:
I am trying to port a simple vector/matrix class library to mingw
(v3.4.2). I have used this library with no problem on MSVC++ v6 for a
number of years. My difficulty is that it does not compile...
|
by: andrew browning |
last post by:
I am getting an istream overloading error that looks like this:
error: no match for 'operator>>' in 'ins >>
target->abrowning_rational::Rational::numerator'
Below is the .h file:
#ifndef...
|
by: karthik |
last post by:
The * operator behaves in 2 different ways. It is used as the value at
address operator as well as the multiplication operator. Does this mean
* is overloaded in c?
|
by: jakester |
last post by:
I am using Visual C++ 2007 to build the code below. I keep getting
linkage error. Could someone please tell me what I am doing wrong? The
code works until I start using namespace for my objects.
...
|
by: KiranJyothi |
last post by:
Can anyone please tell me how can I rectify my compile time error in this program.
/********************************HEADER FILE***********************************/
#ifndef POLYNOMIAL_H //...
|
by: clicwar |
last post by:
A simple program with operator overloading and copy constructor:
#include <iostream>
#include <string>
using namespace std;
class Vector {
private:
float x,y;
public:
Vector(float u, float...
|
by: Chris |
last post by:
Hello all,
Using g++ 3.3.1 on Linux ("Linux From Scratch") I have a data
structure SCSIParams_t that I wish to print out, field-by-field.
Rather than code a long line of the form
std::cout <<...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
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...
|
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: 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,...
|
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,...
|
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...
|
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...
| |