C++ 为什么我需要同时包含 iostream 和 fstream 标头才能打开文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2451681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:32:11  来源:igfitidea点击:

Why do I need to include both the iostream and fstream headers to open a file

c++iostream

提问by skydoor

#include <iostream>
#include <fstream>
using namespace std;

int main () {
  ofstream myfile;
  myfile.open ("test.txt");
  return 0;
}

fstream is derived from iostream, why should we include both in the code above?

fstream 是从 iostream 派生的,为什么我们要在上面的代码中包含两者?

I removed fstream, however, there is an error with ofstream. My question is ofstream is derived from ostream, why fstream is needed to make it compile?

我删除了 fstream,但是,ofstream 有错误。我的问题是 ofstream 是从 ostream 派生的,为什么需要 fstream 来编译它?

回答by Tyler McHenry

You need to include fstreambecause that's where the definition of the ofstreamclass is.

您需要包含,fstream因为那是ofstream类的定义所在。

You've kind of got this backwards: since ofstreamderives from ostream, the fstreamheader includes the iostreamheader, so you could leave out iostreamand it would still compile. But you can't leave out fstreambecause then you don't have a definition for ofstream.

你有点倒退了:由于ofstream派生自ostreamfstream标头包含iostream标头,所以你可以省略iostream它,它仍然会编译。但是您不能遗漏,fstream因为那样您就没有ofstream.

Think about it this way. If I put this in a.h:

这样想想。如果我把这个放进去a.h

class A {
  public:
    A();
    foo();
};

And then I make a class that derives from Ain b.h:

然后我创建了一个派生自Ain的类b.h

#include <a.h>

class B : public A {
  public:
    B();
    bar();
};

And then I want to write this program:

然后我想写这个程序:

int main()
{
  B b;
  b.bar();

  return 0;
}

Which file would I have to include? b.hobviously. How could I include only a.hand expect to have a definition for B?

我必须包含哪个文件?b.h明显地。我怎么能只包括a.h并期望有一个定义B

Remember that in C and C++, includeis literal. It literally pastes the contents of the included file where the includestatement was. It's not like a higher-level statement of "give me everything in this family of classes".

请记住,在 C 和 C++ 中,include是字面量。它实际上将包含文件的内容粘贴到include语句所在的位置。这不像是“给我这个类家族中的一切”的更高层次的陈述。

回答by James McNellis

std::ofstreamis defined in the <fstream>standard library header.

std::ofstream<fstream>标准库头文件中定义。

You need to include that header for its definition so that you can instantiate it.

您需要在其定义中包含该标头,以便您可以实例化它。

回答by M.M

The typedef ofstreamand its associated class template are defined by #include <fstream>, so you need that header.

typedefofstream及其关联的类模板由 定义#include <fstream>,因此您需要该标头。

For your actual program, #include <iostream>is not needed. But you may wish to use your fstreamobject with some functions which operate on ostreamor istreams .

对于您的实际程序,#include <iostream>是不需要的。但是您可能希望将您的fstream对象与一些对ostreamistreams进行操作的函数一起使用。

Those functions are not defined by #include <fstream>and you need to include the right header for any functions you do use. Some implementations might cause #include <fstream>to also include <iostream>but this is not guaranteed by the C++ Standard.

这些函数不是由 定义的#include <fstream>,您需要为您使用的任何函数包含正确的标头。某些实现可能会导致#include <fstream>也包括在内,<iostream>但 C++ 标准不保证这一点。

For example, this code:

例如,这段代码:

ofstream myfile;
myfile.open ("test.txt");

myfile << 1;

requires #include <ostream>(or , since C++11, #include <iostream>which is guaranteed to bring in #include <ostream>).

需要#include <ostream>(或者,自 C++11 起,#include <iostream>保证会引入#include <ostream>)。