Need help with operator overloading. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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; }

8th Feb 2018, 3:04 AM
Camryn Cleveland
Camryn Cleveland - avatar
2 Answers
+ 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
8th Feb 2018, 3:30 AM
jay
jay - avatar
+ 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.
8th Feb 2018, 4:26 AM
Camryn Cleveland
Camryn Cleveland - avatar