Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Operator Overloading : Incorrect implementation being called

Reply
Thread Tools

Operator Overloading : Incorrect implementation being called

 
 
Jim Langston
Guest
Posts: n/a
 
      12-21-2005
"Marcus Kwok" <> wrote in message
news:docgn9$ki3$...
> Jim Langston <> wrote:
>> "Ankit" <> wrote in message
>> news: oups.com...
>>>
>>> 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.


 
Reply With Quote
 
 
 
 
Marcus Kwok
Guest
Posts: n/a
 
      12-21-2005
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
 
Reply With Quote
 
 
 
 
Old Wolf
Guest
Posts: n/a
 
      12-22-2005
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:perator<< . All you can
do is to define :perator<< for ostream and const char*, and
hope that your compiler selects it instead of std:perator<< .
Try writing your call as:
: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.

 
Reply With Quote
 
Ankit
Guest
Posts: n/a
 
      12-22-2005
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

 
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
Operator () overloading, wrong overload gets called fabian.lim@gmail.com C++ 3 09-10-2008 04:09 PM
WebService called by automation dll times out when being called from Navision Felix ASP .Net Web Services 1 09-29-2006 01:43 PM
Operator overloading on "default" operator John Smith C++ 2 10-06-2004 10:22 AM
web service methods being called using incorrect port Jeff Hamilton ASP .Net Web Services 0 09-20-2004 07:05 PM
Incorrect operator<< is called. Terry Santegoeds C++ 2 04-13-2004 07:58 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57