运算符重载:调用不正确的实现 [英] Operator Overloading : Incorrect implementation being called

查看:74
本文介绍了运算符重载:调用不正确的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我有一个旧的VC ++项目代码库,我正在尝试构建并使用
。这使用ostream对象。现在在我的项目中,我已经超载了leftshift运算符(<<<<<<<<<<<<<<<<<<<"数据到流对象的
。但是,当我运行应用程序时,它并没有调用

正确的运算符实现。例如,假设我有以下代码:




ostream o;

int i = 4;

const char * temp =" Test String";


o<< i;

//这会调用正确的实现,即带有int

参数的实现


o<< temp;

//此调用失败。因为它没有调用(const char *)

//实现,而是调用(const double *)实现

//为运算符<<


我不知道为什么会这样。实际上代码驱动一个类形式

ostream并写入而不是写入ostream(如上面的代码中的

)。我编写了一个示例测试程序,它具有与旧代码库中存在的类似的

类层次结构。

中的调用测试程序映射到正确的实现,不像实际的

代码库。


有没有人有关于为什么会发生这种情况的线索,或者我应该如何尝试

并调试这个?我将不胜感激

帮助/意见/建议。


提前多谢,

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

推荐答案



Ankit写道:

Ankit wrote:
你好,
我有一个旧的VC ++项目代码库,我正在尝试构建和使用它。这使用ostream对象。现在在我的项目中,我已经超载了leftshift运算符(<<<<<<<<<<<<<<<<数据到流对象。但是,当我运行应用程序时,它并没有调用
正确的运算符实现。例如,假设我有以下代码:

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

o<< i;
//这会调用正确的实现,即带有int
参数的那个

o<< temp;
//这个电话失败了。因为它没有调用(const char *)
//实现,而是调用(const double *)实现
//为运算符<<

我有不知道为什么会这样。实际上代码驱动一个类形式
ostream并写入,而不是写入ostream(如上面的代码片段)。我编写了一个示例测试程序,其类似于旧代码库中存在的类层次结构。测试程序中的调用映射到正确的实现,不像实际的代码库。

有没有人知道为什么会发生这种情况或者我应该怎么做
并调试这个?我将不胜感激任何帮助/意见/建议。

提前致谢,
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




好吧,我们看不到你的任何代码。是ostream还是ostream&并且

是std :: ostream还是来自已弃用库的旧ostream

< iostream.h> ?



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> ?


Ankit写道:
Ankit wrote:
你好,
我有一个旧的VC ++我正在尝试构建和使用的项目代码库。这使用ostream对象。现在在我的项目中,我已经超载了leftshift运算符(<<<<<<<<<<<<<<<<数据到流对象。但是,当我运行应用程序时,它并没有调用
正确的运算符实现。例如,假设我有以下代码:

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

o<< i;
//这会调用正确的实现,即带有int
参数的那个

o<< temp;
//这个电话失败了。因为它不调用(const char *)
//实现,而是调用(const double *)实现
//为运算符<<



你写了一个运算符<<对于const double *?为什么?

我不知道为什么会这样。实际上代码驱动一个类形式
ostream并写入,而不是写入ostream(如上面的代码片段)。我编写了一个示例测试程序,其类似于旧代码库中存在的类层次结构。测试程序中的调用映射到正确的实现,与实际的代码库不同。
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.




尝试从另一个接近问题方向。减少你的代码直到

错误不再发生。然后最后一步以某种方式连接到

it。



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.


>
你写了一个运营商LT;<对于const double *?为什么?
You wrote an operator<< for const double*? Why?



我认为这是遗留代码。但也许有人会想要

运营商<<对于const double *,其中序列以

NaN值终止。 (我不会,我会使用vector< double>)。


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


这篇关于运算符重载:调用不正确的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆