无法将 'std::ifstream' {aka 'std::basic_ifstream<char>'} 转换为 'bool' 作为回报

1 c++

任何人都可以帮忙吗?我收到标题中的错误

bool fexists(const char *filename)
{
 std::ifstream ifile(filename);
 return ifile;
}
Run Code Online (Sandbox Code Playgroud)

Som*_*ude 5

所述bool转换算子被标记为explicit这意味着它不能在这样的隐式转换使用。

如果流是,则返回good()

return ifile.good();
Run Code Online (Sandbox Code Playgroud)

请注意,在实际条件中使用时,这是一个bool明确需要值的地方,并且将使用转换运算符。


或者考虑您要执行的操作,检查文件是否存在,std::filesystem::exists改为使用。