Jump to content
  • Advertisement
Sign in to follow this  
ZeroEffect

Why do I get this casting error?

This topic is 4943 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When I try to use the += operator between two unsigned chars I get the following error in Visual C++: warning C4244: '+=' : conversion from 'int' to 'unsigned char', possible loss of data However if, using the same variables (both unsigned chars) and the expanded form of the operator (temp = temp + temp2) then I get no errors Thanks.

Share this post


Link to post
Share on other sites
Advertisement
That seems strange. It is possible that operator only works with integers. Maybe you could overload the '+=' operator if that is possible.

Share this post


Link to post
Share on other sites
I can't seem to replicate the problem you have. Could you give us the code you're working with?

You could also try to cast them directly to the unsigned char via foo += (unsigned char)bar; As a last resort, if you're really that desprite, you could disable the warning in VC++ by using #pragma warning(disable:4244) .... I'm pretty sure that's how it is. Haven't done it in a while.

Share this post


Link to post
Share on other sites
I have my warning level set at Level 4(/W4) and it occurs. When I drop the warning level to /W3 the warning is no longer displayed. It still seems odd that the error is displayed in the first place though.

Share this post


Link to post
Share on other sites
You probably get the error because the compiler realises that you can overflow an unsigned char. If you add 1 to 255, you'll get 256, which is 0 as an unsigned char. The compiler is probably converting to an int somewhere, and then converting back to an unsigned char.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!