So, in the course of trying to write my own matrix-class (as an exercise, if nothing else), I ran across the following problem, exemplified in a dummy class:
class dummy
{
public:
int a;
int b;
dummy(int a, int b): a(a), b(b) {};
dummy& operator=(dummy &rhs)
{
a=rhs.a; b=rhs.b;
return *this;
}
dummy operator+(dummy &rhs)
{
dummy out(a+rhs.a,b+rhs.b);
return out;
}
};
Now, there are two versions of main, one that gives an error, and one that doesn't:
error-version:
int main()
{
dummy d1(1,1), d2(0,0);
dummy d3(2,2);
d3=d1+d2;
std::cout << d3.a;
return 0;
}
This gives the following message:
main.cpp:55:error: no match for ‘operator=’ in ‘d3 = d1.dummy::operator+(((dummy&)(& d2)))’
main.cpp:25: note: candidates are: dummy& dummy::operator=(dummy&)
However, if I change the code slightly:
int main()
{
dummy d1(1,1), d2(0,0);
dummy d3=d1+d2;
std::cout << d3.a;
return 0;
}
it works fine.
I also don't get any errors from
int main()
{
dummy d1(1,1), d2(0,0);
d1=d2;
std::cout << d3.a;
return 0;
}
I hope I'm keeping inside the posting procedures, at least I tried my best to.