Troubles Overloading "==" with my class and constant character

Feb 1, 2019 at 9:21am
--
Last edited on Feb 1, 2019 at 10:59pm
Feb 1, 2019 at 9:45am
bool operator == (const char rightside)
You wrote an operator here that accepts a single const char.


w1 == "Apple"

"Apple" is not a single const char. For the purposes of this code, it's effectively a const char*

A const char is not the same as a const char*
Last edited on Feb 1, 2019 at 9:46am
Feb 1, 2019 at 7:49pm
--
Last edited on Feb 1, 2019 at 10:59pm
Feb 1, 2019 at 8:08pm
opperand types are incompatible ("int" and "const char*")

Yeah. Line 16: landOn is an int.
rightside is a const char *. Two very different types.
The compiler error is telling you exactly what is wrong.


Feb 1, 2019 at 9:41pm
--
Last edited on Feb 1, 2019 at 10:59pm
Feb 1, 2019 at 10:16pm
Why did you comment out line 13?
Feb 1, 2019 at 10:27pm
--
Last edited on Feb 1, 2019 at 10:59pm
Feb 1, 2019 at 10:38pm
Well, line 13 would enable to compare the class with a string so that

if (w1 == "Apple")

works.
Feb 1, 2019 at 10:59pm
You seem to have a correspondence between the value of landOn (a number) and the type of plant (a word) that the Cspinner "has":
landOn plant
------------
   0   Apple
   1   Orange
   2   Cherry
   3   Banana
   4   Peach

When you write (for example):
w1 == "Cherry"
the comparison has to (apparently) test whether w1.landOn == 2

(Alternatively, get plant name based on current value of landOn and then compare that name to the other operand.)


How would you "map" between numbers and words so that you can compare?
Topic archived. No new replies allowed.