+ 1

operator overloading C++

#include <iostream> using namespace std; class MyClass { public: int var; MyClass() { } MyClass(int a) : var(a) { } MyClass operator+(MyClass &obj) { MyClass res; res.var= this->var+obj.var; return res; } }; int main() { MyClass obj1(12), obj2(55); MyClass res = obj1+obj2; cout << res.var; } Please helppp. Why missing the ‚ÄúMyClass() { }‚ÄĚin line 7 will cause error??

29th Oct 2019, 2:03 AM
śĪ̝ʮÁēôŚźć
śĪ̝ʮÁēôŚźć - avatar
2 Respuestas
+ 2
Why is it requiredūü§ĒDoes it have any special meaning?
29th Oct 2019, 3:14 AM
śĪ̝ʮÁēôŚźć
śĪ̝ʮÁēôŚźć - avatar
+ 2
Thx~
29th Oct 2019, 3:33 AM
śĪ̝ʮÁēôŚźć
śĪ̝ʮÁēôŚźć - avatar