0

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?

1
  • Two recommendations: Keep all your code in pure English, including the comments. You will present the code to people not capable of your mother tongue (and if only here on SO), for these the comments render useless (unless one can guess, such as pozycja == position – not sure if I had gotten that without the variable names x, y, z). Then avoid commenting the obvious, comments not providing additional information just make reading the code harder; compare int color; // color vs. int color; // as RGB with alpha channel, R at byte 0 Commented May 23, 2021 at 13:41

2 Answers 2

4

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. 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"?

Take a look at integer literals. There it is stated:

The type of the integer literal is the first type in which the value can fit,

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thx for quick reply. How does the compiler know that the value 0xff0000ff is unsigned int, not just int since the signed the leading 1 is the sign bit denoting that this is a negative number?
@Daros911 There are no negative literals in C++.
@Daros911 I added the answer from your comment including the link to the docs. In a short: The first fit is taken... more explained: see above in my answer
@molbdnilo So can I define a negative number?
1

Information technology — Programming languages — C++ 2011

4.5 Integral promotions

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; otherwise, the source prvalue can be converted to a prvalue of type unsigned int.

Therefore,the color field's type should be unsigned int or long or unsigned long,etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.