fstream.h中已经包含iostream.h了,为什么...

local 2012-10-01 09:05:31

#include <fstream>
using namespace std;
int main()
{
int a[10];
ofstream outfile;
outfile.open("student.txt",ios::app);
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
cout<<"enter 10 integer numbers:"<<endl;
for(int i=0;i<10;i++)
{
cin>>a[i];
outfile<<a[i]<<" ";
}
outfile.close();
return 0;
}

fstream.h中已经包含iostream.h了,为什么还提示错误:
'cerr' : undeclared identifier
'cout' : undeclared identifier
...
加上#include <iostream>就对了。
...全文
165 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
local 2012-10-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

引用楼主 的回复:
C/C++ code

#include <fstream>
using namespace std;
int main()
{
int a[10];
ofstream outfile;
outfile.open("student.txt",ios::app);
if(!outfile)
{
cerr<<"open error!"<<end……

……
[/Quote]

哦。Include文件夹里有两个文件FSTREAM和FSTREAM.H。h文件里有#include <iostream.h>。
FSTREAM里只有#include <istream>。
谢谢你。
还是不清楚为什么,学的不够扎实
qq120848369 2012-10-01
  • 打赏
  • 举报
回复
用什么包含什么, 不要依赖头文件的依赖, 你只要做自己想做的, 头文件宏会帮你防止重复引入.
huangxy10 2012-10-01
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
C/C++ code

#include <fstream>
using namespace std;
int main()
{
int a[10];
ofstream outfile;
outfile.open("student.txt",ios::app);
if(!outfile)
{
cerr<<"open error!"<<end……
[/Quote]
fstream 文件中只包含了istream,你要使用输出对象,所以没有找到。
下面是<fstream>中的头部:


// fstream standard header
#pragma once
#ifndef _FSTREAM_
#define _FSTREAM_
#ifndef RC_INVOKED
#include <istream>

#pragma pack(push,_CRT_PACKING)
#pragma warning(push,3)

_STD_BEGIN
#pragma warning(disable: 4127)

extern _MRTIMP2_NCEEPURE _Filet *__CLRCALL_PURE_OR_CDECL _Fiopen(
const char *,
ios_base::openmode, int);
xiaolomg 2012-10-01
  • 打赏
  • 举报
回复
这个,我也不知道

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧