简体   繁体   English

定义简单结构 object

[英]Defining simple struct object

Having such a simple code:有这么简单的代码:

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},
};

I'm getting an error:我收到一个错误:

 };
 ^
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]

the most disturbing for me is the };对我来说最令人不安的是}; error line.错误线。 What's wrong with the code provided?提供的代码有什么问题?

the most disturbing for me is the };对我来说最令人不安的是};

It is only the hint, that the compiler detects the error exact at this place.这只是提示,编译器会在此位置准确检测到错误。 Sometimes this looks wrong, but in this case, it is perfect at the end of the definitions which is the right place.有时这看起来是错误的,但在这种情况下,它在正确位置的定义末尾是完美的。

struct OurVertex
{
  float x, y, z;        // pozycja
  float rhw;            // komponent rhw
  unsigned int color;            // kolor << "unsigned" should fix your problem
};

The value 0xff0000ff is an unsigned int which did not fit into a signed int .0xff0000ff是一个unsigned int ,它不适合有signed int So you simply should define your struct as given above.因此,您只需按照上面给出的方式定义您的结构。

From the comments, additional question: "How does the compiler know that the value 0xff0000ff is unsigned int"?从评论中,附加问题:“编译器如何知道值 0xff0000ff 是无符号整数”?

Take a look at integer literals .看看integer 文字 There it is stated:那里说:

The type of the integer literal is the first type in which the value can fit, integer 文字的类型是该值可以适合的第一个类型,

and the table shows you a column with "Binary, octal, or hexadecimal bases ".该表显示了一个带有“二进制、八进制或十六进制基数”的列。 And for values like 0xff0000ff you see that an unsigned int is the first fit.对于像0xff0000ff这样的值,您会看到unsigned int是第一个合适的值。

Information technology — Programming languages — C++ 2011信息技术 — 编程语言 — C++ 2011

4.5 Integral promotions 4.5 积分促销

A prvalue of an integer type other than bool, char16_t, char32_t, or wchar_t whose integer conversion rank (4.13) is less than the rank of int can be converted to a prvalue of type int if int can represent all the values of the source type; integer 转换等级 (4.13) 小于 int 等级的 integer 转换等级 (4.13) 小于 int 等级的 prvalue 可以转换为 int 类型的源值 int 的所有 int 类型的值; otherwise, the source prvalue can be converted to a prvalue of type unsigned int .否则,源纯右值可以转换为 unsigned int 类型的纯右值

Therefore,the color field's type should be unsigned int or long or unsigned long,etc.因此,颜色字段的类型应该是 unsigned int 或 long 或 unsigned long 等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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