469,926 Members | 1,539 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

operator overloading question

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
Jul 23 '05 #1
8 1797

"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

Jul 23 '05 #2
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)
Jul 23 '05 #3
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
Jul 23 '05 #4
Victor Bazarov wrote:
Node operator+ (Node const & n1, Node const & n2)


Thank you, that fixed it, though I don't understand why....

Jaap Versteegh
Jul 23 '05 #5

"Pete Becker" <pe********@acm.org> wrote in message
news:XM********************@rcn.net...
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>


My mistake. I overlooked the part where this isnt a member function. The solution is to pass the arguments by const&.

--

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

Jul 23 '05 #6
Jaap Versteegh wrote:
Victor Bazarov wrote:
Node operator+ (Node const & n1, Node const & n2)

Thank you, that fixed it, though I don't understand why....


When you return an object, it's a temporary. When you need to pass it as
one of the arguments to another operator+, a reference to non-const cannot
be the argument type, the language prohibits binding a non-const reference
to a temporary. A const reference (or a reference to const, as it is more
correctly known) is allowed to bind to a temporary.

Read more about temporaries and const-correctness.

V
Jul 23 '05 #7
Victor Bazarov wrote:
Read more about temporaries and const-correctness.


Thank you for this advice.

Modifying a temporary is useless, because the effect will be discarded
anyway. So trying to change its value might be an error and therefore the
compiler only allows a constant reference to it.
It's not that the compiler CAN'T handle it, but it refuses to.

This concept is a bit new to me. As you understood, I am relatively new to
C++, having done mostly Delphi. But I like this, it's nice :)

Regards,

Jaap Versteegh
Jul 23 '05 #8
Jaap Versteegh wrote:
[..]
Modifying a temporary is useless, because the effect will be discarded
anyway. So trying to change its value might be an error and therefore
the compiler only allows a constant reference to it.
That's not necessarily true. You are allowed to call a non-const member
function for a temporary.
It's not that the compiler CAN'T handle it, but it refuses to.
I think there are other considerations. I don't remember which, though.
This concept is a bit new to me. As you understood, I am relatively new
to C++, having done mostly Delphi. But I like this, it's nice :)


There are many more things for you to discover in C++. You'll like 'em.
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

30 posts views Thread by | last post: by
2 posts views Thread by victor75040 | last post: by
2 posts views Thread by Bo Sun | last post: by
2 posts views Thread by pmatos | last post: by
1 post views Thread by Tony Johansson | last post: by
7 posts views Thread by Eckhard Lehmann | last post: by
6 posts views Thread by jay | last post: by
5 posts views Thread by Jerry Fleming | last post: by
24 posts views Thread by Rahul | last post: by
3 posts views Thread by Thomas Lenz | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.