You declared the copy constructor as follows:
stri(stri &s1);
This line, in particular the expression on the right side of = , creates a temporary:
stri s3 = s1+s2; // ^^^^^ the result of this expression is a temporary
Since this is copy initialization, she needs to call the copy constructor. But since temporary users cannot communicate with references to non-constant objects, you get an error message.
When you comment on the copy constructor, the compiler generates it for you. His signature then
stri(stri const&);
Now it refers to a constant, and the temporary can communicate with it. The fix should now be obvious.
Please note that even if an accessible copy constructor is required to correctly initialize the copy, the compiler can choose how to reject the call to it during optimization, even if this change changes the observed behavior of your program.
source share