2012-01-30 83 views
0

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

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

,然后我有实际执行FILEIO的方法,但是当我尝试打开这样的文件:

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

我的智能感知允许,和甚至有正确的自动完成,但我的编译器引发的错误:

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()就指出,要打开的文件是否是用于输入,输出,或两者应该是SPECI田间,但是IOS应该会自动通过任何其他的iostream #include指令包括,当我插入这个问题没有解决的:

#include <ios> // directive 

编译器有没有投诉,当我删除第二个说法,但我知道,我应该尝试,并指定以防万一我想从文件中读入并写入文件。我做错什么了吗?

+0

你的意思是'std :: ios'? – wilhelmtell 2012-01-30 05:44:14

+0

是的,我确实需要说明。显然是因为这个文件是在使用namespace指令之前编译的,所以我必须指定它驻留在std中 – gardian06 2012-01-30 05:48:46

回答

4

它看起来像你忘了用std::作为它的前缀,而你还没有使用过using namespace std;(根据你明确说明std::ofstream的名字空间的事实来判断)。

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

您不应该手动需要#include <ios>

2

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

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