C++新手头文件问题
我是 C++ 的初学者,我在头文件管理方面遇到问题。我有一个类 Matrix,它位于 .h 和 .cpp 文件中。我有第二个类 Map,它也有自己的 .h 和 .cpp 文件。 Map.h 包含 Matrix.h,到目前为止它可以工作。但是,当我编写 Map 的实现(在 Map.cpp 中)并使用 Matrix.h 中定义的内容时,它给了我一个未定义的引用错误。包括 Matrix.cpp 确实解决了问题,但我知道这是一个不好的做法。那么,我应该做什么以及为什么?
非常感谢!
编辑:我做了一个测试。我放入Matrix.ha函数声明,在Matrix.cpp中实现它,然后在Map.cpp中使用它。它确实在不包含 Matrix.cpp 的情况下工作。只有属于 Matrix 类的东西才会变得疯狂。
I am a beginner in c++ and I've a problem with header file management. I have a class, Matrix, with it's on .h and .cpp files. I've a second class, Map, with it's own .h and .cpp files too. Map.h includes Matrix.h, and so far it works. But when I go to code the implementation of Map (in Map.cpp) and I use stuff defined in Matrix.h, it gives me a undefined reference error. including Matrix.cpp does resolve the problem, but I know that that's a bad practice. So, what am I supposed to do and why?
Thank you very much!
EDIT: i did a test. i threw in Matrix.h a function declration, Implemented it in Matrix.cpp and then used it in Map.cpp. it did work without including Matrix.cpp. It's only with things belonging to the Matrix class that things go crazy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可能你的问题是你忘记编译Matrix.cpp。 (对我来说)这似乎是一个令人惊讶的常见错误。你如何调用你的编译器?
Probably your problem is that you forgot to compile Matrix.cpp. This seems to be a surprisingly (to me) common mistake. How are you invoking your compiler?
首先我会问一个显而易见的问题;您是否在
Map.cpp
中#include
编辑了Map.h
?我还要指出,
Map.h
可能根本不需要包含Matrix.h
。如果您在 Map.h 中对 Matrix 的唯一引用是指针/引用参数,那么您可以简单地向前声明该类:只要参数是指针或引用类型那么编译器不需要知道矩阵的大小,因此一切正常。如果可以的话,这比在 .h 文件中包含更多标头更好。然后,您将在
Map.cpp
文件中包含Matrix.h
和Map.h
。First I'll ask the obvious; have you
#include
edMap.h
inMap.cpp
?I would also point out that
Map.h
may not need to includeMatrix.h
at all. If your only references to aMatrix
inMap.h
are pointer/reference arguments then you can simply forward declare the class:As long as the argument is a pointer or reference type then the compiler does not need to know the size of a
Matrix
and thus everything just works. This is better than including more headers in your .h files if you can do it. Then you will include bothMatrix.h
andMap.h
in yourMap.cpp
file.您可能希望在编译中包含两个 .h 文件,以便需要执行定义,而不是相互遍历。
然后,您应该链接到 Matrix.o,基于您在 Matrix.cpp 中定义编译 Map.cpp 所需的符号的假设。这是正在发生的事情吗?
You might want to include both .h files in the compilation, in order of need to have definitions work out, instead of through each other.
Then you should link to Matrix.o, based on the assumption that you are defining a symbol in Matrix.cpp that is needed to compile Map.cpp. Is this what is happening?
简单的规则:-
ifndef MATRIX_H
定义 MATRIX_H
...
...头文件的东西
...
endif
在头文件中包含所有类定义/其他声明。相关 .cpp 文件中的定义。
始终尽量避免在一个头文件中包含另一个头文件。
大多数情况下,外部声明就可以了。
链接会自动解析引用。
可以包含 .cpp 文件中的头文件。但在这里,大多数情况下,事情只需要外部声明即可。
Simple rules:-
ifndef MATRIX_H
define MATRIX_H
...
... header file things
...
endif
Have all class definitions/other declarations in header file. Definitions in related .cpp files.
Always try to avoid including one header file from another header file as far as possible.
An extern declaration will do most of the times.
Linked will automatically resolve the reference.
It is ok to include a header file from .cpp file. But here also, most of the times things do only with an extern declaration.