English 中文(简体)
当您需要使用零件时,则需使用零件。
原标题:When do you need to use #include in c++

现在我一直在拟定方案,现在我还没有发现的一件事就是你需要<条码>的编号<>。 我知道,当你使用另一个档案中宣布的内容时,你会这样做。 然而,我有时会发现,我可以删除“/code>,而且所有东西方仍会只汇编罚款。 从我能够告诉的那是,已经列入的其他档案包括外部定义。 有2个特例Im有兴趣了解行为:

  1. Say we have three .h/.cc pairs: f1.h/.cc, f2.h/cc, and f3.h/.cc. 如果f2.h/.cc 包括f1.h和f3.h/.cc 包括f2.h,是否有必要列入f3.h/cc,或者如果将所有f1.h的定义列入f2,那么在f3个档案中就会发现所有f1.h的定义?

  2. 我们再次说,我们有三个:f1.h/cc、f2.h/cc和f3.h/cc。 如果f2包括f1和f2,则f3包括f1或f2之间的“电弧联系”会产生问题?

你们是否知道网上的任何良好资源,我可以读到,以更好地理解在一份档案中包含某些内容如何影响项目随后的档案?

最佳回答

There is nothing much to it. If you use something, you have to include the header declaring the thing you use. About the only exception is forward declaring a class/struct or method like:

class myclass;   

如果你只是需要宣布某一点或提到该类。

你们不能真正依靠其他头脑,包括你们所需要的头盔。 一天,其他负责人的维护者将认识到,他/她不需要再包括和去除。

问题回答

问题1:我认为你再次失踪的是“f2 包括f1”和“f2 是guaranteed以包括f1”。 对于标准负责人来说,这一点尤其重要,因为任何标准负责人都是allowed<>em>,以包括任何其他人员。 因此,如果你依赖间接手段,把这项工作列入你的机器,那么你的代码可能无法汇编不同的C++执行。

如果有一个“f2.h”文件表明或暗示它包含“f1.h”的图书馆,这意味着它始终将采用所有兼容版本,那么你可以依靠间接包括的内容。 如果你重新利用图书馆的一个部分,从根本上依靠该图书馆的另一个组成部分,但其他部分可能被其他用户孤立使用,你可以这样做。 例如,“xhtml_parser.h”可以合理地证明,它提供了“xml_parser.h”和一些额外定义的所有定义。

问题2:总而言之,你是否想重谈这个问题? “f2 包括f1和f2 ,包括f1”你所称的 is,没有“电弧联系”。 www.un.org/french/ecosoc 如果你写上头盔,那么,f1包括f2和f2, 包括f1, 则会造成问题,因为其中含有“链接”,它在很大程度上是其他头盔文件内容的削减和过去。

即便在图3出现之前,通知也可能有问题:

f1.h
----
#ifndef f1_h_included
#define f1_h_included

#include "f2.h"
struct DerivedA : BaseA {};
struct BaseB {};

#endif

f2.h
----
#ifndef f2_h_included
#define f2_h_included

#include "f1.h"
struct BaseA {};
struct DerivedB : BaseB {};

#endif

This isn t going to compile no matter which you include of "f1.h" and "f2.h". Assuming f1 is included first, the result after preprocessing looks like this:

// contents of f2.h, pasted in at line 4 of f1.h
// (contents of f1.h on the circular include are ignored due to include guard)
struct BaseA {};
struct DerivedB : BaseB {};

// rest of f1.h
struct DerivedA : BaseA {};
struct BaseB {};

And so Derived B具体规定了尚未界定的基类。 把它们放在另一个方面,与DerivedA一样。

You ve got it nailed pretty much. In order to use input / output streams I need to include the header file for this. If you wrote a bigint class that supports a big integer type and you sent this class to a friend. Your friend would need to include this in his program in order to make use of it. So you include something when it is not available to your program.

However at times I find that I can remove an #include and everything will still compile just fine. From what I can tell this is because other files being included already are including the external definition.

更正 这只是由于uck而已。

Say we have three .h/.cc pairs: f1.h/.cc, f2.h/.cc, and f3.h/.cc. If f2.h/.cc includes f1.h and f3.h/.cc includes f2.h is it ever necessary for f3.h/.cc to include f1.h or will all of f1.h s definitions be visible to the f3 files when it is included in f2?

您可能是指f1.h 声明,而不是定义,尽管您在其中可能有一些类别和模板功能定义。

无论如何,答案是没有的,永远没有必要。 这些声明将显而易见。 程序前指令只是简单的文字插入。 一旦你发现你的头部,扩大就很容易理解。

Once again say we have three .h/.cc pairs: f1.h/.cc, f2.h/.cc, and f3.h/.cc. If f2 includes f1 and f2 includes f1 and then f3 includes f1 or f2 will the "circular linkage" between and f1 and f2 cause a problem?

是,可能的话, 如果标题档案的contents被打断,就可以减轻这种影响。 但是,如果f2的依靠contents of f1 and 反之亦然,那么你就在法典中具有循环依赖性。 应通过取消这一循环依赖,即:

Do you know of any good resources online I can read to better understand how including something in one file affects subsequent files in the project?

我可以建议:

首先,答案简短,解释如下:

(1) 没有必要在f3.h/cc中宣布“f1.h”为“f1.h”,因为这将带来一种包容周期(不应这样做)

2) in 1->2, 1->2->3 现在没有周期,如果你使用包容保有权,则只包括2个。 I2code #endif

执行工作是具体编辑的,但总体而言是:在钻石包裹中,一个精良的汇编者将统一包容圈子,并在最深层找到档案,该档案包括一些地方,但不需要包括任何其他东西。 然后,它将继续包括列入的档案,而且只能依赖已经列入的档案。 由于这可能导致重复包容,你应始终只包括头盔(如果与同一名录链接而不明确列入),并且还要保护你的主人档案和保 inclusion:

例如,我的头盔:

#ifndef _myheader_h_
#define _myheader_h_
int myglobal;
#endif

如果您的列入有一个周期,则取决于汇编者:它要么试图找到最深层的级别,要么失败,要么选择,要么不试图选择最深的级别,而是按照你在档案中发出的顺序排列;而不管:如果你避免周期(应当)和使用包容的保.,你就处于安全一边。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签