+ 1
Need help with operator overloading.
I couldn’t understand operator overloading so I decided to make a program using operator overloading. When I ran the program a compile error occurred. May someone please help me with this program. #include <iostream> using namespace std; class addfive{ public: int x =0; addfive operator+(addfive num) { this->x += 5; }; }; int main(){ addfive idea; +idea cout<<idea.x; }
2 ответов
+ 8
The way you are using the operator you are overloading indicates to me that you have overloaded the incorrect operator.
To me it seems you want to overload ++ not +
Here is your example but with an overload on ++
https://code.sololearn.com/cZL8cSMx9D08/?ref=app
+ 1
My bad the problem was on my side. For some reason when I ran my code it just said compiler error and not the problem. I just needed to put some semicolons in a few areas and it was fine and I just chose a random operator to use. Thanks for the help though.