I'm new to programming and the topic of header files is sort of bogging me down after I started using a lot of them. In addition to that I'm trying to use precompiled headers. I'm also using the SFML library so I have those headers that also have to be included.
我是编程新手,当我开始大量使用头文件时,头文件的主题让我很困惑。除此之外,我还尝试使用预编译头。我也在使用SFML库,所以我有那些必须包含的头文件。
Right now I have stdafx.h, main.cpp, then classes A, B, C, and D contained within A.h, A.cpp, B.h, B.cpp, C.h, C.cpp, D.h, and D.cpp.
现在我有stdafx。h,主要。然后是A、B、C、D类。h。cpp,B。h,B。cpp,C。h,C。cpp,D。h,D.cpp。
What order would I go about including the headers in all files if
如果我将头文件包含在所有文件中,我将按什么顺序进行
- all classes contain an instance of an SFML class
- 所有类都包含一个SFML类的实例
- class D contains an instance of class A and class C
- 类D包含类A和类C的一个实例
- class C contains an instance of class B My code: (note: all headers have header guards)
- 类C包含类B的实例My code:(注意:所有头文件都有头文件保护)
stdafx.h:
stdafx.h:
#include <SFML/Graphics.hpp>
#include <iostream>
A.h
A.h
#include "stdafx.h"
class A
{
//sfml class
};
A.cpp
A.cpp
#include "stdafx.h"
#include "A.h"
B.h
B.h
#include "stdafx.h"
class B
{
//sfml class
};
B.cpp
B.cpp
#include "stdafx.h"
#include "B.h"
C.h
刘昀
#include "B.h"
class C: public B
{
};
C.cpp
C.cpp
#include "stdafx.h"
#include "C.h"
D.h
D.h
#include "A.h"
#include "C.h"
class D
{
A a;
C C; // if left uncommented I recieve a '1 unresolved externals' error
//sfml class
}
D.cpp
D.cpp
#include "stdafx.h"
#include "D.h"
main.cpp
main.cpp
#include "stdafx.h"
#include "D.h"
5 个解决方案
#1
9
My philosophy is that in well-written code, header files should include all other header files that they depend on. My reasoning is that it should not be possible to include a header file and get a compiler error for doing so. Therefore, each header file should (after the #ifdef
or #pragma once
include guard) include all other headers it depends on.
我的哲学是,在编写良好的代码中,头文件应该包括它们所依赖的所有其他头文件。我的推理是,不应该包含头文件并因此得到编译器错误。因此,每个头文件(在#ifdef或#pragma曾经包含guard之后)应该包含它所依赖的所有其他头文件。
In order to informally test that you've remembered to include the right headers in your header files, *.cpp files should #include the minimum set of header files that should work. Therefore, if there are separate header files for A
, B
, C
and D
, and your cpp file uses class D
, then it should only include D.h. No compiler errors should result, because D.h #include
s A.h and C.h, C.h includes B.h, and A.h and B.h include the SFML header (whatever that is). C.h and D.h can include the SFML header if it seems appropriate, but it's not really necessary, if you can be sure that the dependencies (B.h and A.h) already included it.
为了非正式地测试您记得在头文件中包含正确的头文件,*。cpp文件应该包含应该工作的头文件的最小集合。因此,如果A、B、C和D有单独的头文件,而您的cpp文件使用的是类D,那么它应该只包含D.h。h #包含一个。h和C。h,C。h包括B。h,。h和B。h包括SFML头(不管是什么)。C。h和D。h可以包含SFML头(如果看起来合适的话),但是如果您可以确定依赖项(B),那么它并不是真正需要的。h和A.h)已经包括在内了。
The way Visual C++ does "precompiled headers" screws up this logic, however. It requires you to include "StdAfx.h"
as the very first header file, which causes many developers to simply put all #include
s for the entire project in StdAfx.h, and not to use #include
in any of the other header files. I don't recommend this. Or, they will put all external dependencies in StdAfx.h (e.g. windows.h, boost headers) and #include the local dependencies elsewhere so that changing a single header file does not necessarily cause the entire project to rebuild.
但是,Visual c++“预编译头”的方式破坏了这个逻辑。它要求您包含“StdAfx”。h"作为第一个头文件,这导致许多开发人员将整个项目的所有#include放在StdAfx中。h,不要在任何其他头文件中使用#include。我不推荐这个。或者,他们将把所有外部依赖项放在StdAfx中。h(如windows。h, boost header)和#包含其他地方的本地依赖项,因此更改单个头文件不一定会导致整个项目重新构建。
The way I write my code, most of my CPP files include StdAfx.h, and the corresponding .H file. So A.cpp includes StdAfx.h and A.h, B.cpp includes StdAfx.h and B.h, and so forth. The only other #include
s placed in a cpp file are "internal" dependencies that are not exposed by the header file. For example, if class A
calls printf()
, then A.cpp (not A.h) would #include <stdio.h>
, because A.h does not depend on stdio.h.
我编写代码的方式,我的大多数CPP文件都包含StdAfx。h,对应的。h文件。所以一个。cpp包含StdAfx。h和。h,B。cpp包含StdAfx。h和B。h,等等。cpp文件中唯一包含的其他#是头文件不公开的“内部”依赖项。例如,如果类A调用printf(),那么A。cpp(不是A.h)将#包含
If you follow these rules then the order that you #include
headers does not matter (unless you use precompiled headers: then the precompiled header comes first in each cpp file, but does not need to be included from header files).
如果您遵循这些规则,那么您#include的顺序并不重要(除非您使用预编译头:那么预编译头在每个cpp文件中首先出现,但不需要从头文件中包含)。
#2
1
Take a look at a similar question to learn a good approach to authoring headers.
看一下类似的问题,了解一种编写标题的好方法。
In short, you want to define each header inside a definition guard that prevents headers from being included more then once during compilation. With those in place, for each .h and .cpp file, just include the headers needed to resolve any declarations. The pre-processor and compiler will take care of the rest.
简而言之,您希望在一个定义保护中定义每个头,该保护在编译期间阻止头被包含更多。对于每个.h和.cpp文件,只需包含解决任何声明所需的头文件。预处理器和编译器将负责其余的工作。
#3
1
A.h should include SFML
一个。h应该包括SFML
A.cpp should include A.h
一个。cpp应该包括A.h
D.h should include SFML, A.h and C.h
D。h应该包括SFML, A。h和刘昀
D.cpp should include D.h
D。cpp应该包括D.h
main.cpp should include whichever of A, B, C, D and SFML that it uses directly.
主要。cpp应包括其直接使用的A、B、C、D和SFML中的任何一个。
Generally in a .cpp file I don't include any headers that you know must be included by its corresponding .h because they contain the definitions of data members of classes defined in that .h. Hence, in my code D.cpp would not include A.h. That's just me, though, you might prefer to include it anyway just to remind you that the .cpp file (presumably) uses it.
通常在.cpp文件中,我不包括任何必须包含在相应.h中的头文件,因为它们包含在.h中定义的类的数据成员的定义。因此,在我的代码D中。cpp不会包含A.h,但这只是我,您可能更喜欢包含它,只是为了提醒您.cpp文件(大概)使用它。
This leaves stdafx - where you need this depends what uses the things in it. Probably it's needed everywhere, and MSVC doesn't process (or processes but discards?) anything before #include "stdafx.h"
in a source file, so it should be the first thing in each .cpp file and appear nowhere else.
这就剩下了stdafx——你需要它的地方取决于它的用途。可能在任何地方都需要它,而MSVC在#include“stdafx”之前不会处理(或处理,而是丢弃?)任何东西。h"在源文件中,因此它应该是每个.cpp文件中的第一件事,而不是其他任何地方。
All header files should have multiple-include guards.
所有头文件都应该有多个包含保护。
You could add SFML (or anything else you feel like) to stdafx.h, in which case you might as well remove those includes from everywhere else.
您可以向stdafx添加SFML(或您想要的任何其他东西)。h,在这种情况下,你也可以从其他地方删除这些包含。
Once you've done this, it no longer matters what order you include the headers in each file. So you can do what you like, but I recommend Google's C++ style guide on the subject (click on the arrow thing), adjusted to account for stdafx.h.
一旦您这样做了,那么在每个文件中包含头的顺序就不再重要了。所以你可以做你喜欢的事情,但是我推荐谷歌的c++风格指南(点击箭头),调整为stdafx.h。
#4
1
C inherits class B. So, it should see the identifier B
. So, include B.h
here -
C继承了B类,因此,它应该会看到标识符B。h -
#include "B.h" // Newly added
// Or you can forward declare class B ;
class C: public B
{
};
D has objects of class A
, B
. So, include headers of A, B
in the "D.h" itself.
D有A类的对象,所以,包括A的标头,B在D中。h”本身。
class D
{
A a; // Should see the definition of class A
C c; // Should see the definition of class B
//sfml class
}
D.cpp
D.cpp
#include "A.h"
#include "C.h"
#include "D.h" // Notice that A.h and C.h should definitely placed before
Note that every header needs to be included in it's corresponding source file. Think of each source file independently and see whether what ever is used is earlier defined or not in the source file.
注意,每个头都需要包含在相应的源文件中。独立地考虑每个源文件,看看以前使用的内容是否在源文件中定义过。
#5
0
Depends on dependencies. Unlike C# and other similar languages, C++ does things in the order it is written, so there may be an issue. If you do have a problem with the order then it will not compile.
取决于依赖关系。与c#和其他类似的语言不同,c++按照编写的顺序进行操作,因此可能存在问题。如果您确实遇到了订单的问题,那么它将无法编译。
#1
9
My philosophy is that in well-written code, header files should include all other header files that they depend on. My reasoning is that it should not be possible to include a header file and get a compiler error for doing so. Therefore, each header file should (after the #ifdef
or #pragma once
include guard) include all other headers it depends on.
我的哲学是,在编写良好的代码中,头文件应该包括它们所依赖的所有其他头文件。我的推理是,不应该包含头文件并因此得到编译器错误。因此,每个头文件(在#ifdef或#pragma曾经包含guard之后)应该包含它所依赖的所有其他头文件。
In order to informally test that you've remembered to include the right headers in your header files, *.cpp files should #include the minimum set of header files that should work. Therefore, if there are separate header files for A
, B
, C
and D
, and your cpp file uses class D
, then it should only include D.h. No compiler errors should result, because D.h #include
s A.h and C.h, C.h includes B.h, and A.h and B.h include the SFML header (whatever that is). C.h and D.h can include the SFML header if it seems appropriate, but it's not really necessary, if you can be sure that the dependencies (B.h and A.h) already included it.
为了非正式地测试您记得在头文件中包含正确的头文件,*。cpp文件应该包含应该工作的头文件的最小集合。因此,如果A、B、C和D有单独的头文件,而您的cpp文件使用的是类D,那么它应该只包含D.h。h #包含一个。h和C。h,C。h包括B。h,。h和B。h包括SFML头(不管是什么)。C。h和D。h可以包含SFML头(如果看起来合适的话),但是如果您可以确定依赖项(B),那么它并不是真正需要的。h和A.h)已经包括在内了。
The way Visual C++ does "precompiled headers" screws up this logic, however. It requires you to include "StdAfx.h"
as the very first header file, which causes many developers to simply put all #include
s for the entire project in StdAfx.h, and not to use #include
in any of the other header files. I don't recommend this. Or, they will put all external dependencies in StdAfx.h (e.g. windows.h, boost headers) and #include the local dependencies elsewhere so that changing a single header file does not necessarily cause the entire project to rebuild.
但是,Visual c++“预编译头”的方式破坏了这个逻辑。它要求您包含“StdAfx”。h"作为第一个头文件,这导致许多开发人员将整个项目的所有#include放在StdAfx中。h,不要在任何其他头文件中使用#include。我不推荐这个。或者,他们将把所有外部依赖项放在StdAfx中。h(如windows。h, boost header)和#包含其他地方的本地依赖项,因此更改单个头文件不一定会导致整个项目重新构建。
The way I write my code, most of my CPP files include StdAfx.h, and the corresponding .H file. So A.cpp includes StdAfx.h and A.h, B.cpp includes StdAfx.h and B.h, and so forth. The only other #include
s placed in a cpp file are "internal" dependencies that are not exposed by the header file. For example, if class A
calls printf()
, then A.cpp (not A.h) would #include <stdio.h>
, because A.h does not depend on stdio.h.
我编写代码的方式,我的大多数CPP文件都包含StdAfx。h,对应的。h文件。所以一个。cpp包含StdAfx。h和。h,B。cpp包含StdAfx。h和B。h,等等。cpp文件中唯一包含的其他#是头文件不公开的“内部”依赖项。例如,如果类A调用printf(),那么A。cpp(不是A.h)将#包含
If you follow these rules then the order that you #include
headers does not matter (unless you use precompiled headers: then the precompiled header comes first in each cpp file, but does not need to be included from header files).
如果您遵循这些规则,那么您#include的顺序并不重要(除非您使用预编译头:那么预编译头在每个cpp文件中首先出现,但不需要从头文件中包含)。
#2
1
Take a look at a similar question to learn a good approach to authoring headers.
看一下类似的问题,了解一种编写标题的好方法。
In short, you want to define each header inside a definition guard that prevents headers from being included more then once during compilation. With those in place, for each .h and .cpp file, just include the headers needed to resolve any declarations. The pre-processor and compiler will take care of the rest.
简而言之,您希望在一个定义保护中定义每个头,该保护在编译期间阻止头被包含更多。对于每个.h和.cpp文件,只需包含解决任何声明所需的头文件。预处理器和编译器将负责其余的工作。
#3
1
A.h should include SFML
一个。h应该包括SFML
A.cpp should include A.h
一个。cpp应该包括A.h
D.h should include SFML, A.h and C.h
D。h应该包括SFML, A。h和刘昀
D.cpp should include D.h
D。cpp应该包括D.h
main.cpp should include whichever of A, B, C, D and SFML that it uses directly.
主要。cpp应包括其直接使用的A、B、C、D和SFML中的任何一个。
Generally in a .cpp file I don't include any headers that you know must be included by its corresponding .h because they contain the definitions of data members of classes defined in that .h. Hence, in my code D.cpp would not include A.h. That's just me, though, you might prefer to include it anyway just to remind you that the .cpp file (presumably) uses it.
通常在.cpp文件中,我不包括任何必须包含在相应.h中的头文件,因为它们包含在.h中定义的类的数据成员的定义。因此,在我的代码D中。cpp不会包含A.h,但这只是我,您可能更喜欢包含它,只是为了提醒您.cpp文件(大概)使用它。
This leaves stdafx - where you need this depends what uses the things in it. Probably it's needed everywhere, and MSVC doesn't process (or processes but discards?) anything before #include "stdafx.h"
in a source file, so it should be the first thing in each .cpp file and appear nowhere else.
这就剩下了stdafx——你需要它的地方取决于它的用途。可能在任何地方都需要它,而MSVC在#include“stdafx”之前不会处理(或处理,而是丢弃?)任何东西。h"在源文件中,因此它应该是每个.cpp文件中的第一件事,而不是其他任何地方。
All header files should have multiple-include guards.
所有头文件都应该有多个包含保护。
You could add SFML (or anything else you feel like) to stdafx.h, in which case you might as well remove those includes from everywhere else.
您可以向stdafx添加SFML(或您想要的任何其他东西)。h,在这种情况下,你也可以从其他地方删除这些包含。
Once you've done this, it no longer matters what order you include the headers in each file. So you can do what you like, but I recommend Google's C++ style guide on the subject (click on the arrow thing), adjusted to account for stdafx.h.
一旦您这样做了,那么在每个文件中包含头的顺序就不再重要了。所以你可以做你喜欢的事情,但是我推荐谷歌的c++风格指南(点击箭头),调整为stdafx.h。
#4
1
C inherits class B. So, it should see the identifier B
. So, include B.h
here -
C继承了B类,因此,它应该会看到标识符B。h -
#include "B.h" // Newly added
// Or you can forward declare class B ;
class C: public B
{
};
D has objects of class A
, B
. So, include headers of A, B
in the "D.h" itself.
D有A类的对象,所以,包括A的标头,B在D中。h”本身。
class D
{
A a; // Should see the definition of class A
C c; // Should see the definition of class B
//sfml class
}
D.cpp
D.cpp
#include "A.h"
#include "C.h"
#include "D.h" // Notice that A.h and C.h should definitely placed before
Note that every header needs to be included in it's corresponding source file. Think of each source file independently and see whether what ever is used is earlier defined or not in the source file.
注意,每个头都需要包含在相应的源文件中。独立地考虑每个源文件,看看以前使用的内容是否在源文件中定义过。
#5
0
Depends on dependencies. Unlike C# and other similar languages, C++ does things in the order it is written, so there may be an issue. If you do have a problem with the order then it will not compile.
取决于依赖关系。与c#和其他类似的语言不同,c++按照编写的顺序进行操作,因此可能存在问题。如果您确实遇到了订单的问题,那么它将无法编译。