Hi,
I wrote a simple code about operator overloading. But it can not
compile.
Below is the code:
#include <iostream>
using namespace std;
class A {
friend A operator+(const A &lhs, const A &rhs);
friend istream& operator >>(istream &in, A &hs);
friend ostream& operator <<(ostream &out, const A &hs);
public:
A(): i(10), d(1.11){}
A(int j, double k): i(j), d(k){}
A(A &hs){
cout<<"copy constructor"<<endl;
i = hs.i;
d = hs.d;
}
A& operator=(A &hs){ //LINE18
cout<<"assignment"<<endl;
i = hs.i;
d = hs.d;
return *this;
}
private:
int i;
double d;
};
A operator+(const A &lhs, const A &rhs){
A temp;
temp.i = lhs.i + rhs.i;
temp.d = lhs.d + rhs.d;
return temp;
}
istream& operator >>(istream &in, A &hs){
in >hs.i >hs.d;
return in;
}
ostream& operator <<(ostream &out, const A &hs){
out<<"i: "<<hs.i<<" d: "<<hs.d;
return out;
}
int main(){
A a1;
A a2(5,0.5);
A a3(a2);
a3 = a1;
cout<<" a3--- " <<a3<<endl;
cout<<" a1--- " <<a1<<" a2--- "<<a2<<endl;
a3 = a1 + a2; //LINE 56
cout<<" a3--- " <<a3<<endl;
cin>>a3;
cout<<" a3 after cin--- " <<a3<<endl;
}
The compiling errors are:
overload.cc: In function `int main()':
overload.cc:56: error: no match for 'operator=' in 'a3 =
operator+(const A&,
const A&)((&a2))'
overload.cc:18: error: candidates are: A& A::operator=(A&)
I think this problem is cause by my operator+. I know that in the above
case the synthesized assignment operator should work since there is
not pointers. I just want to learn it.
How to fix the problem? For the operator+, can I implement it as a
friend function? Or must I implement it as a member function?
If possible, please point out other crappy things about my code.
Thanks.
Jack 6 1349 ju******@gmail.com wrote:
Hi,
I wrote a simple code about operator overloading. But it can not
compile.
Below is the code:
#include <iostream>
using namespace std;
class A {
friend A operator+(const A &lhs, const A &rhs);
friend istream& operator >>(istream &in, A &hs);
friend ostream& operator <<(ostream &out, const A &hs);
public:
A(): i(10), d(1.11){}
A(int j, double k): i(j), d(k){}
A(A &hs){
Should be const A&
cout<<"copy constructor"<<endl;
i = hs.i;
d = hs.d;
}
A& operator=(A &hs){ //LINE18
Should be const A&
--
Ian Collins.
Ian Collins wrote:
ju******@gmail.com wrote:
Hi,
I wrote a simple code about operator overloading. But it can not
compile.
Below is the code:
#include <iostream>
using namespace std;
class A {
friend A operator+(const A &lhs, const A &rhs);
friend istream& operator >>(istream &in, A &hs);
friend ostream& operator <<(ostream &out, const A &hs);
public:
A(): i(10), d(1.11){}
A(int j, double k): i(j), d(k){}
A(A &hs){
Should be const A&
cout<<"copy constructor"<<endl;
i = hs.i;
d = hs.d;
}
A& operator=(A &hs){ //LINE18
Should be const A&
Thanks. It works. Could you please explain why?
Another question is: if I define the operator= as below:
A& operator=(const A &hs){
cout<<"assignment"<<endl;
i = hs.i;
d = hs.d;
return *this;
}
Does it mean that it can only accept constant parameters?
For example,
A a1(5, 1.5);
A a2;
a2 = a1;
Here a1 is not a constant variable. But the parameter of operator= is
const. Why does it still work?
Jack ju******@gmail.com wrote:
Ian Collins wrote:
>>ju******@gmail.com wrote:
>>> A& operator=(A &hs){ //LINE18
Should be const A&
Thanks. It works. Could you please explain why?
The result of operator+ is an rvalue (a temporary), so you can not
assign it to a non-const reference.
Another question is: if I define the operator= as below:
A& operator=(const A &hs){
cout<<"assignment"<<endl;
i = hs.i;
d = hs.d;
return *this;
}
Does it mean that it can only accept constant parameters?
No, it's just saying it will use the passed parameter as a const.
--
Ian Collins.
>
The result of operator+ is an rvalue (a temporary), so you can not
assign it to a non-const reference.
Thanks. Why I can not assign a temporary variable to a non-const
reference?
Is it a rule? Sorry, I am a newbie. :-)
Jack
junw2...@gmail.com wrote:
The result of operator+ is an rvalue (a temporary), so you can not
assign it to a non-const reference.
Thanks. Why I can not assign a temporary variable to a non-const
reference?
Is it a rule? Sorry, I am a newbie. :-)
Jack
I believe the answer is because temporary objecst are const by default. ju******@gmail.com wrote:
>>The result of operator+ is an rvalue (a temporary), so you can not assign it to a non-const reference.
Thanks. Why I can not assign a temporary variable to a non-const
reference?
Is it a rule? Sorry, I am a newbie. :-)
Yes, it is a rule.
Conceptually, an rvalue is a value, not an object, so modifying it makes
little sense.
--
Ian Collins. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Brandon J. Van Every |
last post by:
What's better about Ruby than Python? I'm sure there's something. What is
it?
This is not a troll. I'm language shopping and I want people's answers. I
don't know beans about Ruby or have...
|
by: mike420 |
last post by:
I think everyone who used Python will agree that its syntax is
the best thing going for it. It is very readable and easy
for everyone to learn. But, Python does not a have very good
macro...
|
by: Reed L. O'Brien |
last post by:
I see rotor was removed for 2.4 and the docs say use an AES module
provided separately... Is there a standard module that works alike or
an AES module that works alike but with better encryption?...
|
by: Philippe C. Martin |
last post by:
I apologize in advance for launching this post but I might get enlightment
somehow (PS: I am _very_ agnostic ;-).
- 1) I do not consider my intelligence/education above average
- 2) I am very...
|
by: Sarah Tanembaum |
last post by:
Beside its an opensource and supported by community, what's the fundamental
differences between PostgreSQL and those high-price commercial database (and
some are bloated such as Oracle) from...
|
by: lester |
last post by:
a pre-beginner's question:
what is the pros and cons of .net, compared to ++
I am wondering what can I get if I continue to learn C# after I have learned
C --> C++ --> C# ??
I think there...
|
by: Steven T. Hatton |
last post by:
This is something I've been looking at because it is central to a currently
broken part of the KDevelop new application wizard. I'm not complaining
about it being broken, It's a CVS images. ...
|
by: typingcat |
last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so
on. I've tried many PHP IDEs today, but almost non of them supported
Unicode (UTF-8) file.
I've found that the only Unicode...
|
by: Keith K |
last post by:
Having developed with VB since 1992, I am now VERY
interested in C#. I've written several applications with
C# and I do enjoy the language.
What C# Needs:
There are a few things that I do...
|
by: Jason Huang |
last post by:
Hi,
Would someone explain the following coding more detail for me? What's the
( ) for?
CurrentText = (TextBox)e.Item.Cells.Controls;
Thanks.
Jason
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
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,...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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,...
| |