compile error - operator overloading

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Laxman

    compile error - operator overloading

    ==C.H <==
    #include <fstream.h>
    class C
    {
    public:
    C(int i);
    friend ofstream& operator<<(ofst ream& os, const C&);
    };
    ------------------------------------------------------------
    ==B.H <==
    #include <fstream.h>
    #include "C.H"

    class B
    {
    public:
    friend ofstream& operator<<(ofst ream& os, const B&);
    };
    ------------------------------------------------------------
    ==B.C <==
    #include "B.H"

    ofstream& operator<<(ofst ream& 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_ost ream<char, std::char_trait s<char>>::opera tor<<(int)"
    and "operator<<(std ::basic_ofstrea m<char, std::char_trait s<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 ?
  • Laxman

    #2
    Re: compile error - operator overloading

    "MiniDisc_2 k2" <MattDelB@nospa m.com> wrote in message news:<FqEQa.539 9$zd4.3627@lake read02>...[color=blue]
    > "Laxman" <pvlrao@yahoo.c om> wrote in message
    > news:d882854d.0 307141133.6f424 ea7@posting.goo gle.com...[color=green]
    > > ==C.H <==
    > > #include <fstream.h>
    > > class C
    > > {
    > > public:
    > > C(int i);
    > > friend ofstream& operator<<(ofst ream& os, const C&);
    > > };
    > > ------------------------------------------------------------
    > > ==B.H <==
    > > #include <fstream.h>
    > > #include "C.H"
    > >
    > > class B
    > > {
    > > public:
    > > friend ofstream& operator<<(ofst ream& os, const B&);
    > > };
    > > ------------------------------------------------------------
    > > ==B.C <==
    > > #include "B.H"
    > >
    > > ofstream& operator<<(ofst ream& 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_ost ream<char, std::char_trait s<char>>::opera tor<<(int)"
    > > and "operator<<(std ::basic_ofstrea m<char, std::char_trait s<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 ?[/color]
    >
    > 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).[/color]
    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.

    Comment

    • Laxman

      #3
      Re: compile error - operator overloading

      "Steve Pinard" <sNO_SPAMpinard @zoominternet.n et> wrote in message news:<3f134b4a$ 1_6@corp.newsgr oups.com>...[color=blue]
      > 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! =-----[/color]

      Thanks Steve. It works!!

      Comment

      Working...