I have a bit of a problem with an overloaded operator that I'm writing that
I have distilled down to an example in the code below. Essentially, I have
an operator function (+=) that takes a reference parameter of class Poly.
If I use it with a named object (of type Poly), as in:
P1+=P4;
where P1 and P4 are both local variables of the class type, it works fine.
If I try to use an anonymous object on the right side, then I get an error
such as:
error: no match for `Poly& += Poly' operator
This wouldn't be much of a problem if another overloaded operator (+) didn't
return an anonymous copy of a Poly object, so if I write:
P1 += (P2 + P3);
then the RHS is an anonymous Poly object, and the += call won't compile.
This also happens with an overloaded << operator that takes a Poly & if I
try:
cout << (P2 + P3);
I can't find much written about function reference parameters and anonymous
objects. It makes some sense that forming a reference to an object that has
no other name might not be allowed, but I can't find anywhere that says
this. Note also, that this code compiles and works fine in MS Visual C++,
but won't compile on any version of g++ on Linux or the Mac that I've tried.
Any insights on this?
Thanks.
Mark Brandyberry
_________________________________________Poly.h
#include <iostream>
using namespace std;
class Poly {
private:
int i, j, k;
public:
Poly() {i=j=k=0;}
Poly(int x, int y, int z) {i=x;j=y;k=z;}
Poly operator+(Poly &);
void operator+=(Poly &);
friend ostream & operator<<(ostream &, Poly &);
};
________________________________________Poly.cpp
#include "Poly.h"
Poly Poly::operator+(Poly & p) {
Poly t(0,0,0);
t.i=p.i+i;
t.j=p.j+j;
t.k=p.k+k;
return(t);
}
void Poly::operator+=(Poly & p){
i+=p.i; j+=p.j; k+=p.k;
}
ostream & operator<<(ostream & strm, Poly & p) {
strm << "Poly i, j, k = " << p.i << "," << p.j << "," << p.k <<
endl;
return(strm);
}
________________________________________main.cpp
#include "Poly.h"
int main() {
Poly P1, P2(1,2,3), P3(4,5,6), P4;
cout << P1 << P2 << P3;
P1 = P2 + P3;
cout << P1;
P1=P4;
P1+=P2;
cout << P1;
P1=P4;
//the following line will not compile
P1+=(P2+P3);
//the following line will not compile
P1+=Poly(5,10,20);
cout << P1;
P1=P4;
//the following line will not compile
cout << (P2+P3);
return(0);
} 6 3367
"Mark Brandyberry" <md******@uiuc.edu> wrote in message
news:d1**********@news.ks.uiuc.edu I have a bit of a problem with an overloaded operator that I'm writing that I have distilled down to an example in the code below. Essentially, I have an operator function (+=) that takes a reference parameter of class Poly. If I use it with a named object (of type Poly), as in: P1+=P4;
where P1 and P4 are both local variables of the class type, it works fine. If I try to use an anonymous object on the right side, then I get an error such as:
error: no match for `Poly& += Poly' operator
This wouldn't be much of a problem if another overloaded operator (+) didn't return an anonymous copy of a Poly object, so if I write:
P1 += (P2 + P3);
then the RHS is an anonymous Poly object, and the += call won't compile. This also happens with an overloaded << operator that takes a Poly & if I try:
cout << (P2 + P3);
I can't find much written about function reference parameters and anonymous objects. It makes some sense that forming a reference to an object that has no other name might not be allowed, but I can't find anywhere that says this. Note also, that this code compiles and works fine in MS Visual C++, but won't compile on any version of g++ on Linux or the Mac that I've tried. Any insights on this?
Thanks.
Mark Brandyberry
_________________________________________Poly.h #include <iostream> using namespace std;
class Poly { private: int i, j, k; public: Poly() {i=j=k=0;} Poly(int x, int y, int z) {i=x;j=y;k=z;} Poly operator+(Poly &); void operator+=(Poly &); friend ostream & operator<<(ostream &, Poly &); }; ________________________________________Poly.cpp #include "Poly.h"
Poly Poly::operator+(Poly & p) { Poly t(0,0,0); t.i=p.i+i; t.j=p.j+j; t.k=p.k+k; return(t); }
void Poly::operator+=(Poly & p){ i+=p.i; j+=p.j; k+=p.k; }
ostream & operator<<(ostream & strm, Poly & p) { strm << "Poly i, j, k = " << p.i << "," << p.j << "," << p.k << endl; return(strm); } ________________________________________main.cpp #include "Poly.h"
int main() { Poly P1, P2(1,2,3), P3(4,5,6), P4; cout << P1 << P2 << P3; P1 = P2 + P3; cout << P1; P1=P4; P1+=P2; cout << P1; P1=P4; //the following line will not compile P1+=(P2+P3); //the following line will not compile P1+=Poly(5,10,20); cout << P1; P1=P4; //the following line will not compile cout << (P2+P3); return(0); }
What you call "anonymous objects" are temporaries. You cannot supply
temporaries as arguments to functions/operators that take references to
objects as arguments. Your problems will be solved if you simply make the
functions/operators take references to *const* objects as arguments, e.g.,
void operator+=(const Poly &);
void Poly::operator+=(const Poly & p)
{
i+=p.i; j+=p.j; k+=p.k;
}
--
John Carson
The function signature for void Poly::operator+=(Poly & p) should be
Poly &Poly::operator+=(Poly & p);
Ignore my last post I am dreaming.
"John Carson" <jc****************@netspace.net.au> wrote in message
news:d1***********@otis.netspace.net.au... "Mark Brandyberry" <md******@uiuc.edu> wrote in message news:d1**********@news.ks.uiuc.edu I have a bit of a problem with an overloaded operator that I'm writing that I have distilled down to an example in the code below. Essentially, I have an operator function (+=) that takes a reference parameter of class Poly. If I use it with a named object (of type Poly), as in: P1+=P4;
<snip> I can't find much written about function reference parameters and anonymous objects. It makes some sense that forming a reference to an object that has no other name might not be allowed, but I can't find anywhere that says this. Note also, that this code compiles and works fine in MS Visual C++, but won't compile on any version of g++ on Linux or the Mac that I've tried. Any insights on this?
Thanks.
Mark Brandyberry
<snip>
What you call "anonymous objects" are temporaries. You cannot supply temporaries as arguments to functions/operators that take references to objects as arguments. Your problems will be solved if you simply make the functions/operators take references to *const* objects as arguments, e.g.,
void operator+=(const Poly &);
void Poly::operator+=(const Poly & p) { i+=p.i; j+=p.j; k+=p.k; }
-- John Carson
John,
Yes, I knew that what some authors call anonymous objects are temporary,
which is why they're useful. Your response does fix the problem, and I
should have thought of it. I was trying to keep the example code as simple
as possible, so I didn't add the const qualifier. I think this is the first
place I've seen where the const qualifier is actually required, and not just
a good idea. I suppose it does make sense, since now the compiler can be
sure that you aren't going to try to change the reference, and thus change
the anonymous (temporary) object to no good purpose (since it won't be
available when you return from the function anyway). I assume VC++ is just
allowing incorrect code to go through? Or is this really part of the
defined C++ standard? I don't have any experience actually looking at the
standard...
Thanks!
Mark
"Mark Brandyberry" <md******@uiuc.edu> wrote in message
news:d1**********@news.ks.uiuc.edu Yes, I knew that what some authors call anonymous objects are temporary, which is why they're useful. Your response does fix the problem, and I should have thought of it. I was trying to keep the example code as simple as possible, so I didn't add the const qualifier. I think this is the first place I've seen where the const qualifier is actually required, and not just a good idea. I suppose it does make sense, since now the compiler can be sure that you aren't going to try to change the reference, and thus change the anonymous (temporary) object to no good purpose (since it won't be available when you return from the function anyway). I assume VC++ is just allowing incorrect code to go through? Or is this really part of the defined C++ standard? I don't have any experience actually looking at the standard...
Thanks!
Mark
The const is required by the C++ standard. VC++ doesn't require it,
presumably for some backwards compatibility reason.
--
John Carson
mark, take a look at "expression templates". CUJ had 1 or more articles
some time ago.
benedetto
Mark Brandyberry wrote: "John Carson" <jc****************@netspace.net.au> wrote in message news:d1***********@otis.netspace.net.au... "Mark Brandyberry" <md******@uiuc.edu> wrote in message news:d1**********@news.ks.uiuc.edu I have a bit of a problem with an overloaded operator that I'm writing that I have distilled down to an example in the code
below. Essentially, I have an operator function (+=) that takes a
reference parameter of class Poly. If I use it with a named object (of type Poly), as in: P1+=P4;
<snip>
I can't find much written about function reference parameters and anonymous objects. It makes some sense that forming a reference
to an object that has no other name might not be allowed, but I can't find anywhere that says this. Note also, that this code compiles
and works fine in MS Visual C++, but won't compile on any version of
g++ on Linux or the Mac that I've tried. Any insights on this?
Thanks.
Mark Brandyberry <snip>
What you call "anonymous objects" are temporaries. You cannot
supply temporaries as arguments to functions/operators that take
references to objects as arguments. Your problems will be solved if you simply
make the functions/operators take references to *const* objects as
arguments, e.g., void operator+=(const Poly &);
void Poly::operator+=(const Poly & p) { i+=p.i; j+=p.j; k+=p.k; }
-- John Carson
John,
Yes, I knew that what some authors call anonymous objects are
temporary, which is why they're useful. Your response does fix the problem, and
I should have thought of it. I was trying to keep the example code as
simple as possible, so I didn't add the const qualifier. I think this is
the first place I've seen where the const qualifier is actually required, and
not just a good idea. I suppose it does make sense, since now the compiler
can be sure that you aren't going to try to change the reference, and thus
change the anonymous (temporary) object to no good purpose (since it won't
be available when you return from the function anyway). I assume VC++
is just allowing incorrect code to go through? Or is this really part of the
defined C++ standard? I don't have any experience actually looking
at the standard...
Thanks!
Mark This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: lawrence |
last post by:
The following class method is being rejected by the PHP parser. If I
change the method paramaters and allow the objects to be passed as
copies, then the parser has no problem. Or, if I pass by...
|
by: jimjim |
last post by:
Hello,
My question concerns as to how a pointer is passed by reference as a
function argument. The following is from code taken from the MICO
implementation of the CORBA specification.
in...
|
by: Howard |
last post by:
Hi,
in one of the recent posts, I saw someone pass two variables of built-in
type (int and double), by reference, to a member function of a class. That
function then took the addresses of those...
|
by: Steve Jorgensen |
last post by:
When writing VB or VBA code that works with databases or other external
libraries that cannot be trusted to automatically do the right thing when
references to their objects are arbitrarily...
|
by: Mountain Bikn' Guy |
last post by:
It is known that one cannot pass arguments as ref or out in a
marshal-by-reference class. My problem is that I have a C DLL (and C#
wrapper) that I need to isolate in an AppDomain and then I need...
|
by: Max Gay via .NET 247 |
last post by:
I am having a problem with a page that I am trying to develop. Iam new at asp.net.
The page I have created makes a datagrid that has two links aedit and a cancel link. When you click either of the...
|
by: Adriano |
last post by:
Hello,
when I try to print something, either DataGrid or from Crystal Report viever
the folowing error message appears and cancels printing:
Object reference not set to an instance of an...
|
by: Matt B |
last post by:
Hi all,
I am creating a web service which takes in a bulk of training text for
each of two authors, along with a third bulk of anonymous text, and
predicts which author is most likely to have...
|
by: Astley Le Jasper |
last post by:
Sorry for the numpty question ...
How do you find the reference name of an object?
So if i have this
bob = modulename.objectname()
how do i find that the name is 'bob'
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: AndyPSV |
last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
|
by: Arjunsri |
last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
| |