I am trying to override operator+
and operator=
for my classes.
This is my code:
#include <iostream>
#include <vector>
using namespace std;
class Integer {
public:
int i;
Integer operator+ (Integer&);
Integer& operator= (Integer&);
};
Integer Integer::operator+(Integer& rhs) {
Integer res;
res.i = this->i + rhs.i;
return res;
}
Integer& Integer::operator=(Integer& rhs) {
this->i = rhs.i;
return *this;
}
int main()
{
Integer i1, i2, i3;
i1.i = 1, i2.i = 2;
i3 = i1 + i2;
cout << i3.i;
}
In visual studio 2017, the compiler complained that:
"E0349 no operator "=" matches these operands"
It seems that an Integer
object doesn't match with Integer&
in the operator=
function. But it works for operator+
function. It is very confusing.
Thanks a lot.