奇怪的错误:无法从“int”转换到“ios_base::openmode”
我正在使用 g++ 编译一些代码。我写了以下片段:
bool WriteAccess = true;
string Name = "my_file.txt";
ofstream File;
ios_base::open_mode Mode = std::ios_base::in | std::ios_base::binary;
if(WriteAccess)
Mode |= std::ios_base::out | std::ios_base::trunc;
File.open(Name.data(), Mode);
我收到这些错误...知道为什么吗?
错误 1:从“int”到“std::_Ios_Openmode”的转换无效
错误 2:初始化 'std::basic_filebuf<_CharT, _Traits>* std::basic_filebuf<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) 的参数 2 [with _CharT = char, _Traits = std: :char_traits]'
据我从 Google 搜索中得知,g++ 实际上打破了 C++ 标准。我觉得这很令人惊讶,因为它们通常非常严格地遵守标准。是这样吗?或者我做错了什么。
我对标准的参考: http://www.cplusplus.com/reference/iostream/ ofstream/打开/
I am using g++ to compile some code. I wrote the following snippet:
bool WriteAccess = true;
string Name = "my_file.txt";
ofstream File;
ios_base::open_mode Mode = std::ios_base::in | std::ios_base::binary;
if(WriteAccess)
Mode |= std::ios_base::out | std::ios_base::trunc;
File.open(Name.data(), Mode);
And I receive these errors... any idea why?
Error 1: invalid conversion from ‘int’ to ‘std::_Ios_Openmode’
Error 2: initializing argument 2 of ‘std::basic_filebuf<_CharT, _Traits>* std::basic_filebuf<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits]’
As far as I could tell from a Google search, g++ is actually breaking the C++ standard here. Which I find quite astonishing, since they generally conform very strictly to the standard. Is this the case? Or am I doing something wrong.
My reference for the standard: http://www.cplusplus.com/reference/iostream/ofstream/open/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
openmode 是正确的类型,而不是 open_mode。
openmode is the correct type, not open_mode.
g++ 并不完全符合,但这不是这里错误的原因。
模式的类型应该
代替
后者是一个旧的、已弃用的 API。它仍然在 C++ 标准的附录 D 中指定,因此编译器仍然必须支持它。
g++ is not totally conforming, but it's not the reason for the error here.
The type of mode should be
instead of
The latter is an old, deprecated API. It is still specified in Annex D of the C++ standard, so the compiler still have to support it.
这:
应该是:
注意
openmode
中缺少_
。(我必须添加这些行并将代码放入函数中才能编译代码片段。
)
This:
should be:
Note the lack of
_
inopenmode
.(I had to add these lines and put your code in a function to get your snippet to compile.
)