PDA

View Full Version : C++ Casting Error



weems
February 10th, 2011, 01:44 AM
I am having an error when I run the statement l = (int)69 - locationY. It results in a warning converting from int to float error.http://codepad.org/evcZHUwp

MadCow108
February 10th, 2011, 02:26 AM
the error says it: locationY is float and you want to subtract it from an integer.

you must either explicitly cast/ceil/floor/round the float to an integer


69 - (int)locationY
//or
69 - ceilf(locationY)
//etc


or convert the integer to a float:


(float)69 - locationY
//or
69.f - locationY
//or for double precision
69. - locationY


note the the latter will be converted to an int again in the assignment to l.

cgroza
February 10th, 2011, 03:49 AM
Should not that be int(69)? Maybe I missed something?

weems
February 10th, 2011, 04:27 AM
If I cast locationY as an int it causes an infinite loop printing only "|"

nvteighen
February 10th, 2011, 08:26 AM
Should not that be int(69)? Maybe I missed something?

The C casting syntax is (type)argument. C++ inherited that syntax but also added the "constructor" syntax that you mention.


If I cast locationY as an int it causes an infinite loop printing only "|"

Please post some code. (Enclose it between
tags!!)