what is the problem with my code?

J

junw2000

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::eek:perator=(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
 
I

Ian Collins

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&
 
J

junw2000

Ian said:
Should be const A&


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
 
I

Ian Collins

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.
 
J

junw2000

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

Ian Collins

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top