Sign in to follow this  

Why do I get this casting error?

This topic is 4660 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
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
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

This topic is 4660 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this