简体   繁体   中英

C++ - Invalid narrowing conversion from “int” to “unsigned char”

I am trying to to create a vector that contains a static_cast of an interger value as so:

int code = 52;
std::vector<uint8_t> data1 = { 4, 1, 0, 0, 0, 0, 224 + static_cast<uint8_t>(code / 16), static_cast<uint8_t>(code % 16) };

However I am hitting the error:

Invalid narrowing conversion from "int" to "unsigned char"

Can anyone advise why this is?

Thanks

You shall use static_cast to do it. Something like,

static_cast<uint8_t>(224 + code / 16)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM