ios无法识别

发布于 2024-12-29 20:58:04 字数 896 浏览 1 评论 0原文

我正在开发一个执行 fileIO 的 Windows VC++2008 程序,并且遇到了一个非常奇怪的问题。在我的 #include 指令中,我有

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

一个实际上执行 fileIO 的方法,但是当我尝试像这样打开文件时:

std::ofstream Output;
Output.open("Output/log.txt", ios::out);

我的 intelisense 允许它,甚至有正确的自动完成,但我的编译器抛出错误:

1>c:...\engine\gsp420maincore\gsp420maincore\messagequeue.cpp(141) : error C2653: 'ios' : is not a class or namespace name
1>c:...\engine\gsp420maincore\gsp420maincore\messagequeue.cpp(141) : error C2065: 'out' : undeclared identifier

当我读到 ofstream.open() 时,它指出要打开的文件是否用于输入、输出或两者都应指定,但 ios 应该由任何其他 iostream #include 指令自动包含,并且这个问题不是当我插入时更正:

#include <ios> // directive

当我删除第二个参数时,编译器没有任何抱怨,但我知道我应该尝试并指定,以防万一我想进入并从文件中读取以及写入文件。我做错了什么吗?

I am working on a Windows VC++2008 program that does fileIO, and have hit an issue that is really weird. in my #include directives I have

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

and then I have a method that actually does the fileIO, but when I try to open the file like this:

std::ofstream Output;
Output.open("Output/log.txt", ios::out);

my intelisense allows it, and even has correct auto completes, but my compiler throws an error of:

1>c:...\engine\gsp420maincore\gsp420maincore\messagequeue.cpp(141) : error C2653: 'ios' : is not a class or namespace name
1>c:...\engine\gsp420maincore\gsp420maincore\messagequeue.cpp(141) : error C2065: 'out' : undeclared identifier

when I read about the ofstream.open() it stated that whether the file to be opened is for input, output, or both should be specified, but ios should be automatically included by any other iostream #include directive, and this problem is not corrected when I insert the:

#include <ios> // directive

the compiler has no complaints when I remove the second argument, but I know that I should try, and specify just in case I want to go in and read from a file as well as write to it. did I do something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

哆啦不做梦 2025-01-05 20:58:04

看起来您忘记在它前面加上 std:: 前缀,并且没有使用 using namespace std; (从您明确声明 的命名空间这一事实来看>std::ofstream)。

尝试将其更改为 std::ios::out

您不需要手动#include

It looks like you forgot to prefix it with std:: and you haven't used using namespace std; (judging from the fact that you explicitly state the namespace for std::ofstream).

Try changing it to std::ios::out.

You should not need to #include <ios> manually.

梦幻的味道 2025-01-05 20:58:04

正如已经指出的,您需要 std::ios::out ——只是您根本不需要它。当您打开 ofstream 时,它会默认打开以进行输出(同样,默认情况下会打开 ifstream 以进行输入)。我还建议在创建时初始化对象,而不是创建某种未初始化的流,然后单独打开它。考虑到这一点,您会得到相当简单的代码:

std::ofstream Output("output/log.txt");

As already noted, you need std::ios::out -- only you really don't need it at all. When you open an ofstream, it opens for output by default (likewise, an ifstream gets opened for input by default). I'd also advise initializing the object at creation rather than creating a sort-of uninitialized stream, then opening it separately. Taking that into account, you get rather simpler bit of code:

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