在 .cpp 文件中包含头文件

发布于 2025-01-03 01:36:34 字数 237 浏览 3 评论 0原文

我有一个文件 SpreadSheetCell.h 和 SpreadSheetCell.cpp。
我有另一个文件 SpreadSheet.cpp 其中包括 SpreadSheetCell.h 。在这种情况下,它给我错误,如“未定义的引用 SpreadSheetCell:SpreadSheetCell()”等。
但是当我尝试包含 SpreadSheetCell.cpp 时,错误是走了。包含头文件而不是 cpp 文件不是很常见吗?我可能做错了什么?

I have a file SpreadSheetCell.h and SpreadSheetCell.cpp.
I have another file SpreadSheet.cpp which includes SpreadSheetCell.h .In this case it is giving me errors like "undefined reference to SpreadSheetCell:SpreadSheetCell()" etc.
But when i try to include SpreadSheetCell.cpp instead the errors are gone. Is it not common to include the header files rather than the cpp files? What can i possibly be doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

病女 2025-01-10 01:36:34

这是一个链接错误,意味着您在编译时没有包含具有 SpreadSheetCell:SpreadSheetCell() 定义的源文件。

您可能在使用的命令行中缺少 SpreadSheetCell.cpp ,例如:

g++ -o myBinary SpreadSheet.cpp SpreadSheetCell.cpp [...more files?]

That is a linking error and means that you are not including a sourcefile that has the definition for SpreadSheetCell:SpreadSheetCell() when compiling.

You are probably missing SpreadSheetCell.cpp in the command line you use, e.g. something like:

g++ -o myBinary SpreadSheet.cpp SpreadSheetCell.cpp [...more files?]
浅听莫相离 2025-01-10 01:36:34

包含源代码从来都不是一个好主意。始终只包含头文件。

我无法查看您的文件,但是,我认为 SPreadSheetCell.h 包含一个文件(也许是 SpreadSheet.h?),因此存在循环依赖关系。

最好的方法是制作包含的文件的图表(在 cpp 和 h 文件中)。如果某个地方有一个“圆圈”,那么您就发现了问题。在这种情况下,您应该删除一个链接,以便打破圆圈或将文件拆分为较小的文件(也可以删除圆圈)。

(对于“圆圈”,我的意思是例如文件A,包括B,包括C,再次包括A。)

It's never a good idea to include source code. Always only include header files.

I can't look in your file(s), however, I think that SPreadSheetCell.h includes a file (SpreadSheet.h maybe?) so that a circular dependency exist.

The best way is to make a graph of the files that are included (in both cpp and h files). If there is a 'circle' somewhere you have found the problem. In that case you should remove one link so the circle is broken or split files up in smaller files (also to remove the circle).

(with 'circle' I mean e.g. a file A including B including C including A again.)

战皆罪 2025-01-10 01:36:34

确保您的 SpreadSheetCell.cpp 有一个#include "SpreadSheetCell.h",如果没有显示您的代码和编译命令!

Ensure your SpreadSheetCell.cpp has a #include "SpreadSheetCell.h", if not show your code and your compiling commands!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文