Reputation: 435
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?
Upvotes: 0
Views: 53
Reputation: 11
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.
Upvotes: 1
Reputation: 25603
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.
Upvotes: 4