在 .cpp 文件中包含头文件
我有一个文件 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个链接错误,意味着您在编译时没有包含具有
SpreadSheetCell:SpreadSheetCell()
定义的源文件。您可能在使用的命令行中缺少
SpreadSheetCell.cpp
,例如: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:包含源代码从来都不是一个好主意。始终只包含头文件。
我无法查看您的文件,但是,我认为 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.)
确保您的 SpreadSheetCell.cpp 有一个#include "SpreadSheetCell.h",如果没有显示您的代码和编译命令!
Ensure your SpreadSheetCell.cpp has a #include "SpreadSheetCell.h", if not show your code and your compiling commands!