c++报错解决汇总

  1. 如何解决[conversion to 'unsigned char' from 'int' may alter its value]

The compiler is warning you that an int can contain values that doesn’t fit in an unsigned char.

If you are absolutely sure that in this case it will always fit, you can tell that to the compiler by using a static_cast

1
c = static_cast<unsigned char>(i);

The the compiler will trust you on that (and it is your fault if it isn’t true).