VsCode Error with struct

I get the following error, pointing to Rectangle rec height and width, when I instantiate the struct AnimData:

error: non-constant-expression cannot be narrowed from type ‘int’ to ‘float’ in initializer list [-Wc++11-narrowing]

However, the code executes correctly if I press “Debug Anyway”.

Is this a VsCode issue, or should I modify my declaration:

   Texture2D scarfy = LoadTexture("textures/scarfy.png");
   AnimData scarfyData{ 
      {scarfy.width / 6, scarfy.height, 0, 0}, // Rectangle rec
      {w_width / 2 - scarfy.width / 12, w_height - scarfy.height}, // Vector2 pos
      0, // int frame
      1.0 / 12.0, // int updateTime
      0 // int runningTime
   };

As it’s well explained far later in the course, you can convert to float on the fly:

  • Prefix:
(float)scarfy.width
  • Suffix:
12.0f

Odd, normally narrowing-conversions are just a warning than a full-on error. Warnings don’t stop your code from compiling but are meant to let you know there may be a problem when running your code.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms