By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

what is the problem with my code?

P: n/a
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

Jul 2 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.
Jul 2 '06 #2

P: n/a

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

Jul 2 '06 #3

P: n/a
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.
Jul 2 '06 #4

P: n/a
>
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

Jul 2 '06 #5

P: n/a

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.

Jul 2 '06 #6

P: n/a
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.
Jul 2 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.