奇怪的错误:无法从“int”转换到“ios_base::openmode”

发布于 2024-09-03 18:04:02 字数 819 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贱人配狗天长地久 2024-09-10 18:04:02

openmode 是正确的类型,而不是 open_mode。

openmode is the correct type, not open_mode.

坚持沉默 2024-09-10 18:04:02

g++ 并不完全符合,但这不是这里错误的原因。

模式的类型应该

std::ios_base::openmode

代替

std::ios_base::open_mode

后者是一个旧的、已弃用的 API。它仍然在 C++ 标准的附录 D 中指定,因此编译器仍然必须支持它。

g++ is not totally conforming, but it's not the reason for the error here.

The type of mode should be

std::ios_base::openmode

instead of

std::ios_base::open_mode

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.

不可一世的女人 2024-09-10 18:04:02

这:

ios_base::open_mode Mode = std::ios_base::in | std::ios_base::binary;

应该是:

std::ios_base::openmode Mode = std::ios_base::in | std::ios_base::binary;

注意 openmode 中缺少 _

(我必须添加这些行并将代码放入函数中才能编译代码片段。

#include <string>
#include <fstream>

using std::string;
using std::ofstream;
using std::ios_base;

This:

ios_base::open_mode Mode = std::ios_base::in | std::ios_base::binary;

should be:

std::ios_base::openmode Mode = std::ios_base::in | std::ios_base::binary;

Note the lack of _ in openmode.

(I had to add these lines and put your code in a function to get your snippet to compile.

#include <string>
#include <fstream>

using std::string;
using std::ofstream;
using std::ios_base;

)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文