【发布时间】:2013-01-26 11:48:46
【问题描述】:
如何使 ofstream 参数成为可选参数?
bool LookingFor(std::string &mi_name, ofstream &my_file = std::cout)
{
my_file << xxx;
.......
}
上述方法签名的编译错误是:
'std::ofstream& my_file' 具有类型'std::ostream {aka std::basic_ostream}'
我正在使用 mingw32。
我希望这个函数在没有第二个参数时写入控制台。 我尝试了无数的东西,但没有任何效果。 我不介意我是否必须检查代码以查看它是否打开,例如:
if(my_file.isopen())
my_file << xxx;
else
cout << xxx;
有什么好主意吗?
【问题讨论】:
标签: c++ ofstream optional-arguments