473,387 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1961

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: | last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated) before. In general when I have a C++ question I look for answers in "The C++ Programming Language, Third Edition" by Stroustrup....
2
by: victor75040 | last post by:
Before you all start flaming me, I am not a student and this is not for any homework. Just someone learing c++ on their own. I am now up to the chapter in my book that describes operator...
2
by: Bo Sun | last post by:
hi: in the following code: class plus{ int data_item; public:
2
by: pmatos | last post by:
Hi all, I'm overloading operator<< for a lot of classes. The question is about style. I define in each class header the prototype of the overloading as a friend. Now, where should I define the...
1
by: Tony Johansson | last post by:
Hello! I have this wrapper class Integer below that I use when testing operator overloading. A book that I read say that the expression Integer i; i+5 is translated to operator+(i,5) using the...
7
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class...
6
by: jay | last post by:
In the c++ primer ,i get a program. A class's name is TT,and it define the operator overload! TT first; //constructor TT second(30);//constructor TT thrid(40://constructor...
5
by: Jerry Fleming | last post by:
As I am newbie to C++, I am confused by the overloading issues. Everyone says that the four operators can only be overloaded with class member functions instead of global (friend) functions: (), ,...
24
by: Rahul | last post by:
Hi Everyone, I was just overloading operator = for a class and i have a problem in one case... class A { A& operator=(const A& obj) { return *this;
3
by: Thomas Lenz | last post by:
The code below should allow to use a comma instead of << with ostreams and include a space between two operands when comma is used. e.g. cout << "hello", "world", endl; should print the line...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.