fork download
  1. class sample
  2. {
  3. private :
  4. int x;
  5. public :
  6. sample(int x =0)
  7. {
  8. this->x = x;
  9. }
  10. };
  11.  
  12. sample::sample operator+(sample s)
  13. {
  14. this->x = this->x + s.x;
  15. return *this;
  16. }
  17.  
  18.  
  19.  
  20. int main()
  21. {
  22. sample s1(10);
  23. sample s2;
  24. s2 = s2 + s1;
  25. return 0;
  26. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘sample operator+(sample)’:
prog.cpp:14: error: invalid use of ‘this’ in non-member function
prog.cpp:14: error: invalid use of ‘this’ in non-member function
prog.cpp:4: error: ‘int sample::x’ is private
prog.cpp:14: error: within this context
prog.cpp:15: error: invalid use of ‘this’ in non-member function
prog.cpp: In function ‘int main()’:
prog.cpp:24: error: no match for ‘operator+’ in ‘s2 + s1’
prog.cpp:12: note: candidates are: sample operator+(sample)
stdout
Standard output is empty