My center function in class Point below doesn't work as I want it to. In my main, when I reduced 'Point.center().x()' to 'Point.x()', it compiles fine and outputs the correct value. But when center() is involved, comes that issue. I'm not allowed to modify my main. Was just testing on my own. Can anyone tell me where I've gone wrong in my code? Thanks!
Error msg:
request for member ‘x’ in ‘point.Point::center()’, which is of non-class type ‘float’
if (is_near(point.center().x(), x) &&
^
....
center method should not be a float. it should be a point class type. it returns a float, which does not have any fields (you cant say float z; z.x; no such thing.... this is what you are doing). when it returns the correct object, object.x() will work....
Hi again jonnin, thanks for the help. I tried having Point earlier on, it did compile but the output wasn't correct. Maybe I'll try again. so just to clarify on what u just said, the point center() function should return a float? Sorry if I didn't understand properly.
it SHOULD NOT return a float, it MUST return a point.
do you see why?
down in the main
point.center().x … ( pointer.center() ) <--- that is a thing by itself. it is whatever center returns. if its a float, you are done. floats do not have a.x that you can use. if its a point, it makes sense to call .x on it... it has one!
what it *really* does is create a hidden temporary point object (or it will once you fix it!) so you can use the hidden object's x member, copy it out, and the temporary will be gone.