0

我正在使用 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

我在哪里放我的#includes。存储取决于歌曲数据类型,因为它主要是操纵它们、更改标题和移动它们等。如果这似乎是一个新手问题,我很抱歉,但我真的不知道,也没有找到可靠的答案. 我还想声明一个全局常量在实现文件之间共享,这可能吗?

4

2 回答 2

1

My view is that every header file should be possible to include without the programmer having to remember what else to include. This isn't always possible to achieve, but I think it's a good rule to aim at.

In other words, if "storage.h" needs something declared in "song.h", then it should include "song.h" - that way, whoever uses "storage.h" doesn't need to also remember the include of "song.h".

If, say, "storage.h" also uses something from "fstream", then it should include "fstream".

In other words, if you include "storage.h" in a file, that's all you should need to do to use the "storage" class.

于 2013-04-06T11:15:16.580 回答
0

试试这个(对于每个 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

文件并将其包含在需要的地方?

于 2013-04-06T10:51:46.247 回答