Operator Overloading : Incorrect implementation being called

A

Ankit

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
 
E

Earl Purple

Ankit said:
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> ?
 
R

Rolf Magnus

Ankit said:
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.
 
E

Earl Purple

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>).
 
A

Ankit

Hi,

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

Thanks,
Ankit
 
A

Ankit

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.
 
J

Jim Langston

Ankit said:
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";

?
 
M

Marcus Kwok

Jim Langston said:
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?
 
M

Marcus Kwok

Jim Langston said:
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;
}
 
M

Marcus Kwok

Jim Langston said:
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;
}
 
M

Marcus Kwok

Marcus Kwok said:
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.
 
O

Old Wolf

Ankit said:
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.
 
A

Ankit

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top