运算符重载问题 [英] operator overloading question

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

问题描述

我为这样的类重载了加法运算符:

=====

命名空间Geom {


class Node {

...

}


节点运算符+(Node& n1,Node& n2)

{

...

}


void f()

{

节点t1;

节点t2;

节点t3;

节点t4;

t4 = t1 + t2; // ok

t4 = t1 + t2 + t3; //错误

}


}

=========

它产生以下错误:


错误:

''Geom :: operator +(Geom :: Node&, Geom :: Node&)((& t2))+ t3''


有人可以向我解释问题是什么以及如何解决它?


TIA,


Jaap Versteegh

解决方案



" Jaap Versteegh" < J。*********** @ wanadoo.nl>在消息中写道

新闻:Yd ******************** @ casema.nl ...

I已经为这样的类重载了加法运算符:
=====
命名空间Geom {

类节点{
...
}

节点运算符+(Node& n1,Node& n2)
{
...
}


尝试返回对Node对象的引用。引用充当左值和

将有助于进一步级联。

void f()
{Node Node t1;
节点t2 ;节点t3;
节点t4;
t4 = t1 + t2; // ok
t4 = t1 + t2 + t3; //错误
}
}
=========
它产生以下错误:

错误:
''Geom :: operator +(Geom :: Node&,Geom :: Node&)((& t2))+ t3''
有人可以向我解释问题是什么以及如何解决它

TIA,

Jaap Versteegh



Amit写道:

" Jaap Versteegh" < J。*********** @ wanadoo.nl>在消息中写道
新闻:Yd ******************** @ casema.nl ...

I已经为这样的类重载了加法运算符:
=====
命名空间Geom {

类节点{
...
}

节点运算符+(Node& n1,Node& n2)
{
...
}



尝试返回对Node对象的引用。引用充当左值和
,这将有助于进一步级联。




该引用引用的对象是什么? < g>


解决方案是传递const&的参数。


-


Pete Becker

Dinkumware,Ltd。( http:// www。 dinkumware.com


Amit写道:

" Jaap Versteegh" < J。*********** @ wanadoo.nl>在消息中写道
新闻:Yd ******************** @ casema.nl ...

I已经为这样的类重载了加法运算符:
=====
命名空间Geom {

类节点{
...
}

节点运算符+(Node& n1,Node& n2)
{
...
}
尝试返回对节点对象。引用充当左值和
,这将有助于进一步级联。




这不是一个好主意。参考文献涉及什么?一个本地

对象?


更好的解决方案是接受参数作为const

对象的引用:


节点运算符+(节点const& n1,节点const& n2)

void f()
{<节点t1;节点t2;节点t3;节点t4;
t4 = t1 + t2; // ok
t4 = t1 + t2 + t3; //错误
}
}
=========
它产生以下错误:

错误:
''Geom :: operator +(Geom :: Node&,Geom :: Node&)((& t2))+ t3''




V


I have overloaded the addition operator for a class like this:
=====
namespace Geom {

class Node {
...
}

Node operator+ (Node& n1, Node& n2)
{
...
}

void f()
{
Node t1;
Node t2;
Node t3;
Node t4;
t4 = t1 + t2; // ok
t4 = t1 + t2 + t3; //error
}

}
=========
It produces the following error:

error: no match for ''operator+'' in
''Geom::operator+(Geom::Node&,Geom::Node&)((&t2)) + t3''

Could someone please explain to me what the problem is and how to solve it ?

TIA,

Jaap Versteegh

解决方案


"Jaap Versteegh" <j.***********@wanadoo.nl> wrote in message
news:Yd********************@casema.nl...

I have overloaded the addition operator for a class like this:
=====
namespace Geom {

class Node {
...
}

Node operator+ (Node& n1, Node& n2)
{
...
}
Try returning a reference to the Node object. Reference acts as a lvalue and
that would help further cascading.

void f()
{
Node t1;
Node t2;
Node t3;
Node t4;
t4 = t1 + t2; // ok
t4 = t1 + t2 + t3; //error
}

}
=========
It produces the following error:

error: no match for ''operator+'' in
''Geom::operator+(Geom::Node&,Geom::Node&)((&t2)) + t3''

Could someone please explain to me what the problem is and how to solve it ?
TIA,

Jaap Versteegh



Amit wrote:

"Jaap Versteegh" <j.***********@wanadoo.nl> wrote in message
news:Yd********************@casema.nl...

I have overloaded the addition operator for a class like this:
=====
namespace Geom {

class Node {
...
}

Node operator+ (Node& n1, Node& n2)
{
...
}


Try returning a reference to the Node object. Reference acts as a lvalue and
that would help further cascading.



And what object would that reference refer to? <g>

The solution is to pass the arguments by const&.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)


Amit wrote:

"Jaap Versteegh" <j.***********@wanadoo.nl> wrote in message
news:Yd********************@casema.nl...

I have overloaded the addition operator for a class like this:
=====
namespace Geom {

class Node {
...
}

Node operator+ (Node& n1, Node& n2)
{
...
}

Try returning a reference to the Node object. Reference acts as a lvalue and
that would help further cascading.



That''s not a good idea. What would the reference refer to? A local
object?

A better solution would be to accept the arguments as references to const
objects:

Node operator+ (Node const & n1, Node const & n2)

void f()
{
Node t1;
Node t2;
Node t3;
Node t4;
t4 = t1 + t2; // ok
t4 = t1 + t2 + t3; //error
}

}
=========
It produces the following error:

error: no match for ''operator+'' in
''Geom::operator+(Geom::Node&,Geom::Node&)((&t2 )) + t3''



V


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

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