By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,418 Members | 1,082 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,418 IT Pros & Developers. It's quick & easy.

Operator Overloading : Incorrect implementation being called

P: n/a
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

Dec 21 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a

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::ostream or the old ostream from the deprecated library
<iostream.h> ?

Dec 21 '05 #2

P: n/a
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.

Dec 21 '05 #3

P: n/a
>
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>).

Dec 21 '05 #4

P: n/a
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::ostream or the old ostream from the deprecated library
<iostream.h> ?


Dec 21 '05 #5

P: n/a
>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.

Dec 21 '05 #6

P: n/a
"Ankit" <an**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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";

?
Dec 21 '05 #7

P: n/a
Jim Langston <ta*******@rocketmail.com> wrote:
"Ankit" <an**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr 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?

--
Marcus Kwok
Dec 21 '05 #8

P: n/a
Jim Langston <ta*******@rocketmail.com> wrote:
"Ankit" <an**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr 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? For reference, this compiles and runs
perfectly find for me:
#include <iostream>

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

o << temp;

return 0;
}

--
Marcus Kwok
Dec 21 '05 #9

P: n/a
Jim Langston <ta*******@rocketmail.com> wrote:
"Ankit" <an**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr 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? For reference, this compiles and runs
perfectly fine for me:
#include <iostream>

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

o << temp;

return 0;
}

--
Marcus Kwok
Dec 21 '05 #10

P: n/a
"Marcus Kwok" <ri******@gehennom.net> wrote in message
news:do**********@news-int2.gatech.edu...
Jim Langston <ta*******@rocketmail.com> wrote:
"Ankit" <an**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr 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.
Dec 21 '05 #11

P: n/a
Marcus Kwok <ri******@gehennom.net> 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
Dec 21 '05 #12

P: n/a
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::operator<< . All you can
do is to define ::operator<< for ostream and const char*, and
hope that your compiler selects it instead of std::operator<< .
Try writing your call as:
::operator<<(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.

Dec 22 '05 #13

P: n/a
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

Dec 22 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.