有如此简单的代码:
struct OurVertex
{
float x, y, z; // pozycja
float rhw; // komponent rhw
int color; // kolor
};
OurVertex verts[] = {
{ 20.0f, 20.0f, 0.5f, 1.0f, 0xffff0000, },
{ 40.0f, 20.0f, 0.5f, 1.0f, 0xff00ff00, },
{ 20.0f, 40.0f, 0.5f, 1.0f, 0xff00ff55, },
{ 40.0f, 40.0f, 0.5f, 1.0f, 0xff0000ff},
};我收到一个错误:
};
^
main.cpp:47:1: error: narrowing conversion of ‘4278255360u’ from ‘unsigned int’ to ‘int’ inside { } [-Wnarrowing]
main.cpp:47:1: error: narrowing conversion of ‘4278255445u’ from ‘unsigned int’ to ‘int’ inside { } [-Wnarrowing]
main.cpp:47:1: error: narrowing conversion of ‘4278190335u’ from ‘unsigned int’ to ‘int’ inside { } [-Wnarrowing]对我来说最令人不安的是};错误行。提供的代码有什么问题?
发布于 2021-05-23 08:28:33
对我来说最令人不安的是};
这只是提示,编译器在这里准确地检测到错误。有时这看起来是错误的,但在这种情况下,它是完美的,在定义的末尾,这是正确的地方。
struct OurVertex
{
float x, y, z; // pozycja
float rhw; // komponent rhw
unsigned int color; // kolor << "unsigned" should fix your problem
};值0xff0000ff是一个不适合于signed int的unsigned int。因此,您只需像上面所给出的那样定义您的结构。
在注释中,还有一个问题:“编译器如何知道0xff0000ff值是无符号int"?
看看整数字元。报告中指出:
整数文字的类型是第一个值可以适合的类型,
该表向您展示了一列“二进制、八进制或十六进制基”。对于像0xff0000ff这样的值,您可以看到unsigned int是第一个合适的值。
发布于 2021-05-23 09:14:31
信息技术.编程语言. C++ 2011
4.5整体晋升
整数转换秩(4.13)小于int的整数类型( bool、char16_t、char32_t或wchar_t )以外的整数类型的prvalue可以转换为int类型的prvalue,条件是int可以表示源类型的所有值;否则,可以将源prvalue转换为无符号int类型的prvalue。
因此,颜色字段的类型应该是无符号int或长或无符号长等。
https://stackoverflow.com/questions/67657674
复制相似问题