+ 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
5 Answers
+ 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