简体   繁体   中英

make fails with error “cannot convert ‘std::istream {aka std::basic_istream<char>}’ to ‘bool’ in return”

I'm trying to compile libgtextutils (required by the fastxtoolkit) from source. The './configure' command runs nicely, however subsequent 'make' command produces an error that I cannot resolve.

text_line_reader.cpp: In member function ‘bool TextLineReader::next_line()’:
text_line_reader.cpp:47:9: error: cannot convert ‘std::istream {aka std::basic_istream<char>}’ to ‘bool’ in return
  return input_stream ;
         ^~~~~~~~~~~~
make[3]: *** [text_line_reader.lo] Error 1
make[2]: *** [all-recursive] Error 1
make[1]: *** [all-recursive] Error 1
make: *** [all] Error 2

I'm on a Mac, OSX 10.11.6 (Intel)

Any suggestions that might solve this are highly appreciated.

See the Porting to GCC 6 guide, which documents this as one of the changes you must deal with due to GCC 6 defaulting to C++14 mode instead of C++03 mode:

Cannot convert 'std::ostream' to 'bool'

As of C++11, iostream classes are no longer implicitly convertible to void* so it is no longer valid to do something like:

 bool valid(std::ostream& os) { return os; }

Such code must be changed to convert the iostream object to bool explicitly, eg return (bool)os; or return static_cast<bool>(os);

Another option is to explicitly use -std=c++03 in your compiler flags to compile in C++03 mode, but it's better to fix the code. The fixes given above will make the code compatible with any C++ version.

This is because of using a newer version of gcc compiler (C++11).

Use static_cast<bool>() to resolve the issue.

example: change

success = move_group.move();

to

success = static_cast<bool>(move_group.move());

Based on the answers of both Muthanna and Jonathan, for anyone with absolutely no knowledge of C++ but wanting to install fastx-toolkit on *unix and coming across these challenging responses, the answer is to run this after unpacking the source code:

cd libgtextutils-0.7
sed -i '47s/input_stream/static_cast<bool>(input_stream)/' src/gtextutils/text_line_reader.cpp
./configure
make
make install

This makes the change that the other, clearly far more knowledgeable, respondents suggest making, without having to understand a word that they're talking about. This should make it work for those of us who are just trying to get some old software to install and aren't looking to learn how to write C++ (just yet...)

Another option is to override the C++ flags in the make command:

make CXXFLAGS='-std=c++03 -O1'

As noted above , fastx will also have a compilation error but it will compile if you pass it

make CXXFLAGS=-O1 .

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