I'm trying to compile https://github.com/jbeder/yaml-cpp on a Raspberry Pi, and I'm getting this error:
/home/pi/yaml-cpp/test/binary_test.cpp:11:38: error: narrowing conversion of ‘-58’ from ‘int’ to ‘char’ [-Wnarrowing]
11 | std::string input{-58, -1, -99, 109};
| ^
I gather that I should explicitly declare the numbers inside the bracket as signed chars, as some older compilers might not do so by themselves. But I am wholly unfamiliar with this syntax: What does this line actually do?
Related
the file I want to compile
first error:
cannot convert 'wchar_t*' to 'LPCSTR' {aka 'const char*'}|
I fixed it by changing WriteConsoleOutputCharacter to WriteConsoleOutputCharacterW.
However I get another error on this line:
swprintf(screen, 40, L"X=%3.2f, Y=%3.2f, A=%3.2f FPS=%3.2f ", fPlayerX, fPlayerY, fPlayerA, 1.0f/fElapsedTime);
invalid conversion from 'int' to 'const wchar_t*' [-fpermissive]|
initializing argument 2 of 'int swprintf(wchar_t*, const wchar_t*, ...)'|
How can I compile this code?
The swprintf function you're using does not appear to take a buffer size. You may want to consider using _snwprintf instead.
_snwprintf(screen, 40, L"X=%3.2f, Y=%3.2f, A=%3.2f FPS=%3.2f ", fPlayerX, fPlayerY, fPlayerA, 1.0f/fElapsedTime);
You may get warned that this function is unsafe - the suggested alternative is _snwprintf_s where you must specify both the size of the buffer and the maximum number of characters to write. In practice, these are often the same, so most of the time, calls to this function look like this:
_snwprintf_s(screen, 40, 40, L"X=%3.2f, Y=%3.2f, A=%3.2f FPS=%3.2f ", fPlayerX, fPlayerY, fPlayerA, 1.0f/fElapsedTime);
Consider the following code:
#include <complex>
int main()
{
unsigned u = 1u;
auto result = static_cast<std::complex<int>>(u);
return 0;
}
Compiling with
g++ -std=c++11 -Werror -Wsign-conversion -o a.out source_file.cpp
Causes compile error
source_file.cpp: In function ‘int main()’:
source_file.cpp:8:51: error: conversion to ‘int’ from ‘unsigned int’ may change the sign of the result [-Werror=sign-conversion]
auto result = static_cast<std::complex<int>>(u);
^
clang reports a similar error
source_file.cpp:6:50: error: implicit conversion changes signedness: 'unsigned int' to 'const value_type' (aka 'const int') [-Werror,-Wsign-conversion]
auto result = static_cast<std::complex<int>>(u);
~~~~~~~~~~~ ^
The error does not make much sense at first sight, what am I missing?
You get a conversion warning not from the cast, but from inside construction of std::complex.
To 'fix' your example you should instead do:
auto result = std::complex<int>{static_cast<int>(u)};
I am trying to compile a really old software in linux debian 9.5, i keep getting this error:
janpdf/PDF.cpp: In member function ‘void PDF::OpenFile(const char*)’:
janpdf/PDF.cpp:41:74: error: narrowing conversion of ‘199’ from ‘int’ to
‘char’ inside { } [-Wnarrowing]
char signature[] = {'%', '%', 'G' + 128, 'R' + 128, 'A' + 128, '\n', 0};
^
janpdf/PDF.cpp:41:74: error: narrowing conversion of ‘210’ from ‘int’ to
‘char’ inside { } [-Wnarrowing]
janpdf/PDF.cpp:41:74: error: narrowing conversion of ‘193’ from ‘int’ to
‘char’ inside { } [-Wnarrowing]
Makefile:153: recipe for target 'janpdf/PDF.o' failed
make: *** [janpdf/PDF.o] Error 1
I have already tried teh signed / unsigned 'char'approach. Although I know almos nothing about coding this is the only answer I found. Any other solution is welcome.
thanks
Well, apparently in your implementation values like 210 and 199 do not fit into the range of type char. So, the conversion is narrowing. {} initializers do not allow narrowing conversions.
This suggests that your implementation apparently uses signed char type.
You can forcefully convert the values to char by using explicit casts inside the {}. You can stop using {} initializers. You can force your implementation to use unsigned char. There are many "solutions" for this problem, but there's no way to chose one without more context.
If the code was originally written for the same "family" of implementations you are compiling it on now, then most likely it was simply written for an older version of the language, which performed that narrowing conversion implicitly. In that case to reproduce the old behavior you'll need explicit casts
char signature[] =
{'%', '%', (char) ('G' + 128), (char) ('R' + 128), (char) ('A' + 128), '\n', 0};
The lowest-effort way to get your thing to build is probably to add -Wno-narrowing to your compiler invocation. If you're using make, you can probably start it with something like CFLAGS=-Wno-narrowing make (assuming you're using bash) to get the desired effect.
Current compilers use by default newer versions of C++. Your compiler may be trying to compile the source in C++11 or C++14 mode.
Try adding -std=c++03 to your compiler flags.
I have a very simple 'issue' with LibSVM:
I am on a Mac, OSX, 10.9.5. I cloned the libSVM repo from here, and as per the instructions on the README, simply ran make.
This is supposed to compile the libSVM programs without issue, but I am getting a litany of warnings about signedness.
Needless to say, this concerns me, because of signs are changing willy nilly, I will have no faith in the classifier. Should this be something of concern, and if so, how do I go about resolving it?
Here is what I get when I run the make command from the command line:
adam-MacBook-Pro:libsvm adam$ make
c++ -Wall -Wconversion -O3 -fPIC -c svm.cpp
svm.cpp:96:26: warning: implicit conversion changes signedness: 'int' to 'size_t' (aka 'unsigned long') [-Wsign-conversion]
head = (head_t *)calloc(l,sizeof(head_t)); // initialized to 0
~~~~~~ ^
svm.cpp:98:10: warning: implicit conversion changes signedness: 'int' to 'unsigned long' [-Wsign-conversion]
size -= l * sizeof(head_t) / sizeof(Qfloat);
^ ~
svm.cpp:146:54: warning: implicit conversion changes signedness: 'int' to 'unsigned long' [-Wsign-conversion]
h->data = (Qfloat *)realloc(h->data,sizeof(Qfloat)*len);
~^~~
svm.cpp:1651:38: warning: implicit conversion changes signedness: 'const int' to 'unsigned long' [-Wsign-conversion]
double *alpha = Malloc(double,prob->l);
~~~~~~~~~~~~~~~~~~~~^~
svm.cpp:39:40: note: expanded from macro 'Malloc'
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
^
svm.cpp:1722:26: warning: implicit conversion changes signedness: 'int' to 'unsigned long' [-Wsign-conversion]
double *t=Malloc(double,l);
~~~~~~~~~~~~~~^~
svm.cpp:39:40: note: expanded from macro 'Malloc'
#define Malloc(type,n) (type *)malloc((n)*sizeof(type))
^
svm.cpp:1833:29: warning: implicit conversion changes signedness: 'int' to 'unsigned long' [-Wsign-conversion]
I have a huge list of errors that come up when i try to compile source code under cygwin.. My best approach to learning programming is hitting it hard, and trail and error. So even if my C++ knowledge is very basic, I am still really new, so please when you explain can I please ask that you use baby talk for a lack of a better word lol. When I type in 'make' under the source directory is gives me these errors. A friend of mine, we are friends on a MUD, he has been a programmer for 35 years and he says to me that the compiler is not liking that the function is returning a pointer and to change all the "return ''''" to return strdup('''')
Please let me know what you guys think. Thanks
Underneath is only a very smalllll part of the syntax that was given to me after I typed in make in Cygwin. I hope someone has the time to explain this to me, thank you.
$ make
make -s smaug
-Compiling o/imc.o....
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
};
^
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write--strings]
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
imc.c:106:1: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
imc.c: In function ‘char* color_itom(const char*, CHAR_DATA*)’:
imc.c:393:14: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
return "";
^
imc.c: In function ‘char* color_mtoi(const char*)’:
imc.c:414:14: error: deprecated conversion from string constant to ‘char*’ [-Werror=write-strings]
return "";
^
imc.c: In function ‘char* imccapitalize(const char*)’:
imc.c:525:35: error: conversion to ‘char’ from ‘int’ may alter its value [-Werror=conversion]
strcap[i] = tolower( str[i] );
^
imc.c:527:35: error: conversion to ‘char’ from ‘int’ may alter its value [-Werror=conversion]
strcap[0] = toupper( strcap[0] );
^
imc.c: In function ‘void imc_new_channel(const char*, const char*, const char*, const char*, const char*, bool, int, const char*)’:
imc.c:1089:13: error: conversion to ‘short int’ from ‘int’ may alter its value [-Werror=conversion]
c->level = perm;
^
^
cc1plus: all warnings being treated as errors
Makefile:101: recipe for target 'o/imc.o' failed
make[1]: *** [o/imc.o] Error 1
Makefile:46: recipe for target 'all' failed
make: *** [all] Error 2
Ok below is the code where it shows error for line 106: 1 and 393: Its a very lonnng .c file I am sure you guys don't want to upload the whole thing, but here is the portion of it, and according to Visual 2013 here is starting point line 106 and 393: i am not sure when cygwin says the line number where the error took place if that doesn't include white space and comments, but here is 106 and 393 according to VS:
line 106
SITEINFO *this_imcmud;
line 393
if( IMCIS_SET( IMCFLAG( ch ), IMC_COLORFLAG ) )
You should show your code, but your problems are:
Somewhere you're doing something like:
char *x = "hello";
It should be:
const char *x = "hello";
Similarly, char* color_itom(const char*, CHAR_DATA*) should return const char * if you want to return string literals from it.
strcap is defined as a char array, but you're putting the int values returned by tolower and toupper in there somewhere. Either change the type or put in an explicit cast.
Same for c->level = perm. Either add an explicit cast or change the type of c->level to match the type of perm.
The error:
deprecated conversion from string constant to ‘char*’
is caused by using string literal to initiaize char*, e.g.
char* str = "something";
This should read:
const char* str = "something";