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&,G eom::Node&)((&t 2)) + t3'
Could someone please explain to me what the problem is and how to solve it ?
TIA,
Jaap Versteegh 8 1993
"Jaap Versteegh" <j.***********@ wanadoo.nl> wrote in message
news:Yd******** ************@ca sema.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&,G eom::Node&)((&t 2)) + 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******** ************@ca sema.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******** ************@ca sema.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::operat or+(Geom::Node& ,Geom::Node&)(( &t2)) + t3'
V
Victor Bazarov wrote: Node operator+ (Node const & n1, Node const & n2)
Thank you, that fixed it, though I don't understand why....
Jaap Versteegh
"Pete Becker" <pe********@acm .org> wrote in message
news:XM******** ************@rc n.net... Amit wrote: "Jaap Versteegh" <j.***********@ wanadoo.nl> wrote in message news:Yd******** ************@ca sema.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)
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
However, I've come upon a question that I can neither answer from
"The Book" or a Google search (so yes, at least I RTFBed). I'm
hoping that someone in this news group might know the answer.
Overloading the Operator
Say I want to develop a...
|
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
overloading.
I just cannot find any explanation that clearly point out what the
parts of the statement refer to. For example the book says:
comp operator+(comp b)
|
by: Bo Sun |
last post by:
hi:
in the following code:
class plus{
int data_item;
public:
|
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 overloading of operator<<.
In the .cc of the respective class or in a file where I am overloading
operator<< for all classes?
Cheers,
|
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 stand-alone helper function.
If Integer's constructor were not declared as explicit, the previous would
have one more possible translation which is
operator+(i, Integer(5))
| |
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 xydata with operator- overloaded:
class xydata {
public:
|
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
first=second.operator+;
the question is the fourth line is all right?
|
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: (), , ->, =. I wonder
why there is such a restriction.
Some tutorials say that 'new' and 'delete' can only be overloaded with
static member functions, others say that all overloading function should
be non-static. Then what is the fact, and why?
...
|
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;
|
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 "hello world".
#include <iostream>
using namespace std;
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |