Help - operator overloading | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help - operator overloading

I am trying to apply the instructions according to the overload but it gives me an error in the output. I need help with this exercise. The problem mentions the following: You and your friend are playing a game as a team. Each player must pass 2 rounds and earn points for each of them. The given program creates two Score objects where the scores for each round are stored (passed to the constructor). Overload the + operator for the Score class to calculate the team's score for each round. Remember to use the operator keyword while overloading the operator. This is my code: https://code.sololearn.com/c7qj079Ck5H3/?ref=app

7th Jul 2021, 3:02 PM
Alex Narváez
Alex Narváez - avatar
5 Answers
+ 3
Alex Narváez There should be r1.round1Score + r2.round1Score; r1.round2Score + r2.round2Score; ------- public static Score operator+(Score r1, Score r2) { int res1 = r1.round1Score + r2.round1Score; int res2 = r1.round2Score + r2.round2Score; Score res = new Score(res1, res2); return res; }
7th Jul 2021, 3:18 PM
A͢J
A͢J - avatar
+ 1
Alex Narváez I verified your updated code and it worked fine.
7th Jul 2021, 3:31 PM
A͢J
A͢J - avatar
+ 1
Okay, copy the code I had here from the original one and change it and now it worked for me. Very strange but now it did work. Thank you I ᴀᴍ "Tɪᴍᴇ"
7th Jul 2021, 3:52 PM
Alex Narváez
Alex Narváez - avatar
0
Correct, however it does not work for me in the module, I get an error even though it seems to work in the test editor. I don't know what is wrong or missing
7th Jul 2021, 3:17 PM
Alex Narváez
Alex Narváez - avatar
0
I ᴀᴍ "Tɪᴍᴇ" I already tried, and modify the code. You can verify it, the output seems to be fine but in the exam module it goes wrong. I don't understand why it generates an error
7th Jul 2021, 3:25 PM
Alex Narváez
Alex Narváez - avatar