Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > compile error - operator overloading

Reply
Thread Tools

compile error - operator overloading

 
 
Laxman
Guest
Posts: n/a
 
      07-14-2003
==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>>:perator<<(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 ?
 
Reply With Quote
 
 
 
 
Laxman
Guest
Posts: n/a
 
      07-15-2003
"MiniDisc_2k2" <(E-Mail Removed)> wrote in message news:<FqEQa.5399$zd4.3627@lakeread02>...
> "Laxman" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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>>:perator<<(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.
 
Reply With Quote
 
 
 
 
Laxman
Guest
Posts: n/a
 
      07-15-2003
"Steve Pinard" <(E-Mail Removed)> wrote in message news:<3f134b4a$(E-Mail Removed)>...
> 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!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overloading operator->*() and operator->() gob00st@googlemail.com C++ 11 02-20-2009 08:52 PM
user defined conversion operator or operator overloading? hurcan solter C++ 3 08-29-2007 07:39 PM
Why is overloading operator. (member operator) forbidden? dascandy@gmail.com C++ 11 05-16-2007 07:54 PM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM



Advertisments