[英]Is #include <../header.h> a bad practice in C++?
My c++ project in summary is composed of two directories and of course the main.cpp
: 总的来说,我的c ++项目由两个目录组成,当然还有
main.cpp
:
Dataloader
containing dataloader.h and dataloader.cpp Dataloader
含有dataloader.h和dataloader.cpp Analysis
containing analysis.h and analysis.cpp Analysis
含analysis.h和analysis.cpp in dataloader.h I included analysis.h as follows: 在dataloader.h中,我包含了analysis.h ,如下所示:
#include<../Analysis/analysis.h>
My professor told me that it's a BIG NO NO!!!!!. 我的教授告诉我,这是很大的不! We were trying to create a makefile and he was surprised.
我们试图创建一个makefile ,他感到惊讶。 Why is it a bad practice?
为什么这是一个坏习惯? and how can I make the include simpler.
以及如何使包含变得更简单。 I am working on
code::blocks
and gcc 4.8 compiler. 我正在研究
code::blocks
和gcc 4.8编译器。
NB I noticed that code::blocks some how forgives include errors.
注意:我注意到code ::阻止了一些宽恕如何包含错误。 When we tried to compiled it by using gcc command it went bad
当我们尝试使用gcc命令对其进行编译时,它变得很糟糕
I agree with your professor. 我同意你的教授。 Use of
用于
#include<../Analysis/analysis.h>
makes the code brittle. 使代码变脆。 If the code base is re-organized using a different directory structure, files containing such
#include
statements will fail to compile. 如果使用不同的目录结构重新组织了代码库,则包含此类
#include
语句的文件将无法编译。
Regarding: 关于:
NB I noticed that code::blocks some how forgives include errors.
注意:我注意到code ::阻止了一些宽恕如何包含错误。 When we tried to compiled it by using gcc command it went bad
当我们尝试使用gcc命令对其进行编译时,它变得很糟糕
Add -I.
添加
-I.
to the compiler flags to resolve that problem. 编译器标志来解决该问题。
Well I've always been taught, and this makes sense, that you should put your includes in the file that depends on them. 好吧,我一直都被教导,这很有意义,您应该将包含内容放入依赖它们的文件中。
So in your situation, I assume you use dataloader
to import data, and then the analysis
to analyze it. 因此,根据您的情况,我假设您使用数据
dataloader
器导入数据,然后使用analysis
对其进行分析。 Well in this case, it makes more sense to include both of these in the main. 好了,在这种情况下,将两者都包含在主体中就更有意义了。
In comparison, I made an n by n tic tac toe game that includes a print_board()
function. 相比之下,我制作了一个n到n井字游戏,其中包含
print_board()
函数。 This function relies on a header file with constants, colors.h
, to properly print the board. 此功能依赖于带有常量
colors.h
的头文件来正确打印电路板。 So in this circumstance, it makes more sense for my print_board.h
file to include the colors.h
file, because it is directly dependent on it, and nothing else is. 因此,在这种情况下,使我的
print_board.h
文件包含colors.h
文件更加有意义,因为它直接依赖于该文件,而没有其他内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.