当使用多个文件时,我把预处理器命令放在哪里?
Where do I put preprocessor commands when using multiple files?
我正在用c++为学校做一个项目,这个项目要分成多个文件。
test_driver.cpp - a file to test the code written
storage.cpp - implementation file for storage class and methods
storage.h - header file for storage
song.cpp - implementation file for song class, songs are the data type being manipulated by storage
song.h - header file for song
我把我的#include放在哪里?存储依赖于歌曲数据类型,因为它主要是操作它们,更改标题和移动它们等。如果这看起来像一个新问题,我很抱歉,但我真的不知道,也没有找到一个可靠的答案。我还想声明一个全局常数之间的实现文件共享,这是可能的吗?
我的观点是,每个头文件都应该可以包含,而程序员不必记住还要包含什么。这并不总是能够实现,但我认为这是一个很好的规则。
换句话说,如果"storage.h"需要在"song.h"中声明的内容,那么它应该包含"song.h"——这样,使用"storage.h"的人就不需要记住"song.h"的包含。
如果"storage.h"也使用了"fstream"中的内容,那么它应该包含"fstream"。
换句话说,如果在文件中包含"storage.h",那么使用"storage"类所需要做的就是这些。
试试这个(对于每个cpp文件,我列出了您应该包含在其中的h文件):
song.cpp
song.h
storage.cpp
storage.h
song.h
或者,如果storage.h也依赖于song.h(即,它使用其中的定义),您可以这样做:
storage.h
song.h
storage.cpp
storage.h
对于常量,为什么不定义一个
呢?constants.h
文件并在需要的地方包含它?
相关文章:
- 错误:无效的预处理指令 #i 的意思是 #if?
- C++预处理会生成变量成员、资源库和映射
- 使用预处理指令检查是否包含标头?
- 预处理的 C/C++ 文件是否特定于计算机?
- 使用 GCC 对 C 文件进行部分预处理(不删除 "define" 指令)
- 在 CPLEX 中求解线性规划,无需剪切和预处理
- CPP -D 选项,用于预处理 Fortran 代码
- 错误:粘贴"tmp_UINT"和"+"未提供有效的预处理令牌
- 任务计划程序库的预处理不起作用 - 多定义错误
- Eclipse 问题 - 编译期间不考虑 .c 和 .cpp 文件中定义的预处理
- 使用python预处理后,C++(opencv)中的垫子类型数据与image_to_array相同
- Howo 使用 cl 预处理为 masm 组装生成一个单独的文件
- 我有一个预处理的 C/C++ 源文件 (cacti.i).如何从这个 .i 文件生成可执行二进制文件,以便我可以像 ./
- 如何使用Visual Studio C/C++编译器(cl.exe)来预处理我的objective-C代码
- 是具有预处理前分支实现的结构违反ODR
- 与不完整的Cholesky预处理的共轭梯度返回特征库的意外错误
- Visual Studio C - 无法输出预处理文件
- 在海湾合作委员会中加快宏观预处理的任何方法
- 所有结构成员的预处理器批处理
- 当头文件被预处理时是否有一个预处理器选项显示?