PDA

View Full Version : Bizarre Conditional Operator Problem in C++


kingofsquirrels
2006.05.23, 06:18 PM
Hi all,

Today I encountered a very strange problem. It appears that the conditional '>' operator is not working correctly in the following code:


if (velocity.x() > MAX_VELOCITY_WHILE_RUNNING_ON_GROUND);
{
printf("(velocity.x = %f) > (max_vel = %f)\n\n", velocity.x(), MAX_VELOCITY_WHILE_RUNNING_ON_GROUND);
velocity.setX(MAX_VELOCITY_WHILE_RUNNING_ON_GROUND );
}


The output of the printf() function is: (velocity.x = -2.666667) > (max_vel = 40.000000).

velocity.x() is an inline function that simply returns a private _x variable. MAX_VELOCITY_WHILE_RUNNING_ON_GROUND is a static const float set to 40.0.

Any ideas what might be causing this? I'm using XCode 2.1 with GCC 4.0.

Thanks!
-- Will

Taxxodium
2006.05.23, 06:51 PM
if (velocity.x() > MAX_VELOCITY_WHILE_RUNNING_ON_GROUND);

remove the semi colon at the end and it should work.

kingofsquirrels
2006.05.23, 06:56 PM
Wow, I definitely should have seen that. Thanks for the prompt reply!