简体   繁体   中英

error: narrowing conversion of ‘199’ from ‘int’ to ‘char’ inside { } [-Wnarrowing]

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM