Operator Overloading : Incorrect implementation being called

Discussion in 'C++' started by Ankit, Dec 21, 2005.

  1. Ankit

    Ankit Guest

    Hello,

    I have an old VC++ project code base which I am trying to build and
    use. This uses an ostream object. Now in my project, I have overloaded
    the leftshift operator ( << ), basically being used to "put" data to
    the stream object. However, while I run the app, it does not call the
    correct implementation of the operator. For example, say I have
    following piece of code:

    ostream o;
    int i = 4;
    const char* temp = "Test String";

    o << i;
    // this calls the correct implementation, i.e. the one which takes int
    argument

    o << temp;
    // this call fails. because it does not call the (const char*)
    //implementation, rather calls (const double*) implementation
    //for the operator <<

    I have no clue why this happens. Actually the code drives a class form
    ostream and writes to that rather than writing to ostream (as in the
    above piece of code). I wrote a sample test program with a similar
    class hierarchy as the one present in the old code base. The calls in
    the test program map to the correct implementations, unlike the actual
    code base.

    Does anyone has a clue as to why this could happen or how should I try
    and debug this? I will appreciate any kind of
    help/comments/suggestions.

    Thanks a ton in advance,
    Ankit
     
    Ankit, Dec 21, 2005
    #1
    1. Advertising

  2. Ankit

    Earl Purple Guest

    Ankit wrote:
    > Hello,
    >
    > I have an old VC++ project code base which I am trying to build and
    > use. This uses an ostream object. Now in my project, I have overloaded
    > the leftshift operator ( << ), basically being used to "put" data to
    > the stream object. However, while I run the app, it does not call the
    > correct implementation of the operator. For example, say I have
    > following piece of code:
    >
    > ostream o;
    > int i = 4;
    > const char* temp = "Test String";
    >
    > o << i;
    > // this calls the correct implementation, i.e. the one which takes int
    > argument
    >
    > o << temp;
    > // this call fails. because it does not call the (const char*)
    > //implementation, rather calls (const double*) implementation
    > //for the operator <<
    >
    > I have no clue why this happens. Actually the code drives a class form
    > ostream and writes to that rather than writing to ostream (as in the
    > above piece of code). I wrote a sample test program with a similar
    > class hierarchy as the one present in the old code base. The calls in
    > the test program map to the correct implementations, unlike the actual
    > code base.
    >
    > Does anyone has a clue as to why this could happen or how should I try
    > and debug this? I will appreciate any kind of
    > help/comments/suggestions.
    >
    > Thanks a ton in advance,
    > Ankit


    Well we can't see any of your code. Is o an ostream or an ostream & and
    is it std::eek:stream or the old ostream from the deprecated library
    <iostream.h> ?
     
    Earl Purple, Dec 21, 2005
    #2
    1. Advertising

  3. Ankit

    Rolf Magnus Guest

    Ankit wrote:

    > Hello,
    >
    > I have an old VC++ project code base which I am trying to build and
    > use. This uses an ostream object. Now in my project, I have overloaded
    > the leftshift operator ( << ), basically being used to "put" data to
    > the stream object. However, while I run the app, it does not call the
    > correct implementation of the operator. For example, say I have
    > following piece of code:
    >
    > ostream o;
    > int i = 4;
    > const char* temp = "Test String";
    >
    > o << i;
    > // this calls the correct implementation, i.e. the one which takes int
    > argument
    >
    > o << temp;
    > // this call fails. because it does not call the (const char*)
    > //implementation, rather calls (const double*) implementation
    > //for the operator <<



    You wrote an operator<< for const double*? Why?

    > I have no clue why this happens. Actually the code drives a class form
    > ostream and writes to that rather than writing to ostream (as in the
    > above piece of code). I wrote a sample test program with a similar
    > class hierarchy as the one present in the old code base. The calls in
    > the test program map to the correct implementations, unlike the actual
    > code base.


    Try to approach the problem from the other direction. Reduce your code until
    the error doesn't occur anymore. Then the last step is somehow connected to
    it.
     
    Rolf Magnus, Dec 21, 2005
    #3
  4. Ankit

    Earl Purple Guest

    >
    > You wrote an operator<< for const double*? Why?
    >

    I think it's legacy code. But maybe someone would want to have
    operator<< for const double * where the sequence is terminated with a
    NaN value. (I wouldn't, I'd use vector<double>).
     
    Earl Purple, Dec 21, 2005
    #4
  5. Ankit

    Ankit Guest

    Hi,

    I apologize for being unclear. The project uses ostream&, which has
    been derived from iostream.h.

    Thanks,
    Ankit


    Earl Purple wrote:
    > Ankit wrote:
    > > Hello,
    > >
    > > I have an old VC++ project code base which I am trying to build and
    > > use. This uses an ostream object. Now in my project, I have overloaded
    > > the leftshift operator ( << ), basically being used to "put" data to
    > > the stream object. However, while I run the app, it does not call the
    > > correct implementation of the operator. For example, say I have
    > > following piece of code:
    > >
    > > ostream o;
    > > int i = 4;
    > > const char* temp = "Test String";
    > >
    > > o << i;
    > > // this calls the correct implementation, i.e. the one which takes int
    > > argument
    > >
    > > o << temp;
    > > // this call fails. because it does not call the (const char*)
    > > //implementation, rather calls (const double*) implementation
    > > //for the operator <<
    > >
    > > I have no clue why this happens. Actually the code drives a class form
    > > ostream and writes to that rather than writing to ostream (as in the
    > > above piece of code). I wrote a sample test program with a similar
    > > class hierarchy as the one present in the old code base. The calls in
    > > the test program map to the correct implementations, unlike the actual
    > > code base.
    > >
    > > Does anyone has a clue as to why this could happen or how should I try
    > > and debug this? I will appreciate any kind of
    > > help/comments/suggestions.
    > >
    > > Thanks a ton in advance,
    > > Ankit

    >
    > Well we can't see any of your code. Is o an ostream or an ostream & and
    > is it std::eek:stream or the old ostream from the deprecated library
    > <iostream.h> ?
     
    Ankit, Dec 21, 2005
    #5
  6. Ankit

    Ankit Guest

    >You wrote an operator<< for const double*? Why?

    As Earl Purple just said, its actually a legacy code which was written
    long back. I am unsure as to why did they use << for const double* at
    that point of time.
     
    Ankit, Dec 21, 2005
    #6
  7. Ankit

    Jim Langston Guest

    "Ankit" <> wrote in message
    news:...
    > Hello,
    >
    > I have an old VC++ project code base which I am trying to build and
    > use. This uses an ostream object. Now in my project, I have overloaded
    > the leftshift operator ( << ), basically being used to "put" data to
    > the stream object. However, while I run the app, it does not call the
    > correct implementation of the operator. For example, say I have
    > following piece of code:
    >
    > ostream o;
    > int i = 4;
    > const char* temp = "Test String";
    >
    > o << i;
    > // this calls the correct implementation, i.e. the one which takes int
    > argument
    >
    > o << temp;
    > // this call fails. because it does not call the (const char*)
    > //implementation, rather calls (const double*) implementation
    > //for the operator <<


    Perhaps your problem is coming from the fact that temp is declared as a
    const. const means it won't change, yet here you're trying to change it.

    Does it work if you change it to:
    char* temp = "Test String";

    ?
     
    Jim Langston, Dec 21, 2005
    #7
  8. Ankit

    Marcus Kwok Guest

    Jim Langston <> wrote:
    > "Ankit" <> wrote in message
    > news:...
    >>
    >> I have an old VC++ project code base which I am trying to build and
    >> use. This uses an ostream object. Now in my project, I have overloaded
    >> the leftshift operator ( << ), basically being used to "put" data to
    >> the stream object. However, while I run the app, it does not call the
    >> correct implementation of the operator. For example, say I have
    >> following piece of code:
    >>
    >> ostream o;
    >> int i = 4;
    >> const char* temp = "Test String";
    >>
    >> o << temp;
    >> // this call fails. because it does not call the (const char*)
    >> //implementation, rather calls (const double*) implementation
    >> //for the operator <<

    >
    > Perhaps your problem is coming from the fact that temp is declared as a
    > const. const means it won't change, yet here you're trying to change it.


    Where is he changing temp?

    --
    Marcus Kwok
     
    Marcus Kwok, Dec 21, 2005
    #8
  9. Ankit

    Marcus Kwok Guest

    Jim Langston <> wrote:
    > "Ankit" <> wrote in message
    > news:...
    >>
    >> I have an old VC++ project code base which I am trying to build and
    >> use. This uses an ostream object. Now in my project, I have overloaded
    >> the leftshift operator ( << ), basically being used to "put" data to
    >> the stream object. However, while I run the app, it does not call the
    >> correct implementation of the operator. For example, say I have
    >> following piece of code:
    >>
    >> ostream o;
    >> int i = 4;
    >> const char* temp = "Test String";
    >>
    >> o << temp;
    >> // this call fails. because it does not call the (const char*)
    >> //implementation, rather calls (const double*) implementation
    >> //for the operator <<

    >
    > Perhaps your problem is coming from the fact that temp is declared as a
    > const. const means it won't change, yet here you're trying to change it.


    Where is he changing temp? For reference, this compiles and runs
    perfectly find for me:


    #include <iostream>

    int main()
    {
    std::eek:stream& o = std::cout;
    const char* temp = "Test String";

    o << temp;

    return 0;
    }



    --
    Marcus Kwok
     
    Marcus Kwok, Dec 21, 2005
    #9
  10. Ankit

    Marcus Kwok Guest

    Jim Langston <> wrote:
    > "Ankit" <> wrote in message
    > news:...
    >>
    >> I have an old VC++ project code base which I am trying to build and
    >> use. This uses an ostream object. Now in my project, I have overloaded
    >> the leftshift operator ( << ), basically being used to "put" data to
    >> the stream object. However, while I run the app, it does not call the
    >> correct implementation of the operator. For example, say I have
    >> following piece of code:
    >>
    >> ostream o;
    >> int i = 4;
    >> const char* temp = "Test String";
    >>
    >> o << temp;
    >> // this call fails. because it does not call the (const char*)
    >> //implementation, rather calls (const double*) implementation
    >> //for the operator <<

    >
    > Perhaps your problem is coming from the fact that temp is declared as a
    > const. const means it won't change, yet here you're trying to change it.


    Where is he changing temp? For reference, this compiles and runs
    perfectly fine for me:


    #include <iostream>

    int main()
    {
    std::eek:stream& o = std::cout;
    const char* temp = "Test String";

    o << temp;

    return 0;
    }



    --
    Marcus Kwok
     
    Marcus Kwok, Dec 21, 2005
    #10
  11. Ankit

    Jim Langston Guest

    "Marcus Kwok" <> wrote in message
    news:docgn9$ki3$...
    > Jim Langston <> wrote:
    >> "Ankit" <> wrote in message
    >> news:...
    >>>
    >>> I have an old VC++ project code base which I am trying to build and
    >>> use. This uses an ostream object. Now in my project, I have overloaded
    >>> the leftshift operator ( << ), basically being used to "put" data to
    >>> the stream object. However, while I run the app, it does not call the
    >>> correct implementation of the operator. For example, say I have
    >>> following piece of code:
    >>>
    >>> ostream o;
    >>> int i = 4;
    >>> const char* temp = "Test String";
    >>>
    >>> o << temp;
    >>> // this call fails. because it does not call the (const char*)
    >>> //implementation, rather calls (const double*) implementation
    >>> //for the operator <<

    >>
    >> Perhaps your problem is coming from the fact that temp is declared as a
    >> const. const means it won't change, yet here you're trying to change it.

    >
    > Where is he changing temp?


    o_O Why the heck did I read that as o >> temp;?

    Just ignore me. I seem to be 0 for 2 today.
     
    Jim Langston, Dec 21, 2005
    #11
  12. Ankit

    Marcus Kwok Guest

    Marcus Kwok <> wrote:
    > Where is he changing temp? For reference, this compiles and runs
    > perfectly fine for me:


    Sorry for the multiple posts. Apparently Google Groups does not honor
    supercedes.

    --
    Marcus Kwok
     
    Marcus Kwok, Dec 21, 2005
    #12
  13. Ankit

    Old Wolf Guest

    Ankit wrote:

    > Hello,
    >
    > I have an old VC++ project code base which I am trying to build and
    > use. This uses an ostream object. Now in my project, I have overloaded
    > the leftshift operator ( << ), basically being used to "put" data to
    > the stream object. However, while I run the app, it does not call the
    > correct implementation of the operator. For example, say I have
    > following piece of code:
    >
    > ostream o;
    > int i = 4;
    > const char* temp = "Test String";
    >
    > o << i;
    >
    > o << temp;
    > // this call fails. because it does not call the (const char*)
    > //implementation, rather calls (const double*) implementation
    > //for the operator <<


    Well, that's not possible -- char* is not convertible to double*
    without a cast. Do you perhaps means that it is calling the void*
    overload?

    Please post your exact code that implements the overloads
    and calls them. The above code can't be correct (o can never
    be put to any useful purpose).

    It's not possible to add overloads to std::eek:perator<< . All you can
    do is to define ::eek:perator<< for ostream and const char*, and
    hope that your compiler selects it instead of std::eek:perator<< .
    Try writing your call as:
    ::eek:perator<<(o, temp);
    and see if it then chooses the right functions.

    Also, include <iostream> (not iostream.h which is a non-standard
    header). You might also find things easier if you don't do a "using
    namespace std;" until you get your problem sorted out.
     
    Old Wolf, Dec 22, 2005
    #13
  14. Ankit

    Ankit Guest

    Hi all,

    Thanks for all your suggestions. I figured out that the problem was
    being caused because the library where the operator << was defined, was
    using a /J (default char is unsigned) option while compilation.

    This lib was included in the DLL that I was trying to make. However,
    the DLL did not have /J option for its build. Thus, the confusion was
    being caused due to signed/unsigned chars.

    Thanks once again for all your suggestions. I did learn about operator
    overloading from the posts :)

    Regards,
    Ankit
     
    Ankit, Dec 22, 2005
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Terry Santegoeds

    Incorrect operator<< is called.

    Terry Santegoeds, Apr 11, 2004, in forum: C++
    Replies:
    2
    Views:
    380
  2. John Smith
    Replies:
    2
    Views:
    433
    Ivan Vecerina
    Oct 6, 2004
  3. Replies:
    3
    Views:
    415
    James Kanze
    Sep 10, 2008
  4. Jeff Hamilton

    web service methods being called using incorrect port

    Jeff Hamilton, Sep 20, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    122
    Jeff Hamilton
    Sep 20, 2004
  5. Felix
    Replies:
    1
    Views:
    348
    Felix
    Sep 29, 2006
Loading...

Share This Page