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: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 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: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