469,926 Members | 1,539 Online

=====
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...
=====
namespace Geom {

class Node {
...
}

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

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...
=====
namespace Geom {

class Node {
...
}

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

Try returning a reference to the Node object. Reference acts as a lvalue and

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...
=====
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'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...
=====
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.

V
Jul 23 '05 #7
Victor Bazarov 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.
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.