1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class sample { private : int x; public : sample(int x =0) { this->x = x; } }; sample::sample operator+(sample s) { this->x = this->x + s.x; return *this; } int main() { sample s1(10); sample s2; s2 = s2 + s1; return 0; } |
Y2xhc3Mgc2FtcGxlCnsKcHJpdmF0ZSA6IAogIGludCB4OwpwdWJsaWMgOgogIHNhbXBsZShpbnQgeCA9MCkKICB7CiAgICB0aGlzLT54ID0geDsKICB9Cn07CgpzYW1wbGU6OnNhbXBsZSBvcGVyYXRvcisoc2FtcGxlIHMpCnsKICB0aGlzLT54ID0gdGhpcy0+eCArIHMueDsKICByZXR1cm4gKnRoaXM7Cn0KCgoKICBpbnQgIG1haW4oKQogICAgewogICAgICBzYW1wbGUgczEoMTApOwogICAgICBzYW1wbGUgczI7CiAgICAgIHMyID0gczIgKyBzMTsKICAgICAgcmV0dXJuIDA7ICAgIAogICAgfQ==
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)
-
result: Compilation error (maybe you wish to see an example for C++ 4.7.2)


