4
votes

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;
}
1
See this question here 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

1 Answers

3
votes

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.