4

I am trying to compile hosts3d from sourceforge and it does compile but generates several narrowing errors. I don't have any idea how to fix this but any help would be greatly appreciated. I suspect I could download the previous version of the compiler and i might end up doing that but for now... c++11

g++ -Wall -O2 -c -o src/glwin.o src/glwin.cpp src/glwin.cpp: In member function 'int MyGLWin::AddInput(int, int, unsigned int, int, const char*, bool)': src/glwin.cpp:983:147: warning: narrowing conversion of 'max' from 'int' to 'unsigned int' inside { } is ill-formed in C++11 [-Wnarrowing] glin_obj glin = {GLWIN_INPUT, lastWin, lower, names++, left, top, cwidth, strlen(text), (strlen(text) > cwidth ? strlen(text) - cwidth : 0), max};

//create input object, return name of input object
int MyGLWin::AddInput(int left, int top, unsigned int cwidth, int max, const char *text, bool lower)
{
  if (!lastWin) return -1;  //check parent window object exists
  glin_obj glin = {GLWIN_INPUT, lastWin, lower, names++, left, top, cwidth, strlen(text), (strlen(text) > cwidth ? strlen(text) - cwidth : 0), max};
  strcpy(glin.text, text);  //default text
  currInput = glin.name;  //set input object focus (for keys)
  lastInput = (glin_obj *)GLWinLL.Write(new glin_obj(glin));
  return glin.name;
}
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
user3182829
  • 41
  • 1
  • 2
  • 1
    See this question [here](http://stackoverflow.com/q/31685132/1708801) narrowing conversions in `{}` are ill-formed in C++11 but were not previously. gcc for several versions made it a warning but currently is an error. – Shafik Yaghmour Nov 08 '15 at 03:56

1 Answers1

3

First this is "only" a warning, so you might be able to ignore it if you understand what happens behind. Here the value int max (signed) will be automatically converted into a unsigned int.

Secondly, as the function AddInput seems to be part of the API, you can't modify the signature of the function. So if you really want to remove the warning (without removing the effect explained earlier), you could force the cast with (unsigned int)max.

A. Lefebvre
  • 139
  • 4
  • Yeah that helps a lot, I just started using MINGW and seeing this warning and I saw examples where others had forced the cast but not an int in an API. I am having fun modifying some open source. Which led me to read this and this. http://stackoverflow.com/questions/4975340/int-to-unsigned-int-conversion http://stackoverflow.com/questions/50605/signed-to-unsigned-conversion-in-c-is-it-always-safe http://stackoverflow.com/questions/809227/is-it-safe-to-use-1-to-set-all-bits-to-true Thanks again, Joe – user3182829 Jan 12 '14 at 02:33