简体   繁体   中英

Cannot convert 'std::ifstream' {aka 'std::basic_ifstream<char>'} to 'bool' in return

Can anyone help? I'm getting the error in the title

bool fexists(const char *filename)
{
 std::ifstream ifile(filename);
 return ifile;
}

The bool conversion operator is marked as explicit which means it can't be used in implicit conversions like this.

Instead return if the stream is good() :

return ifile.good();

Note that when use in an actual condition then that's a place where a bool value is explicitly wanted and the conversion operator will be used.


Or considering the operation you want to do, check for file existence, use std::filesystem::exists instead.

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