C++新手头文件问题

发布于 2024-12-01 11:14:08 字数 371 浏览 0 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(4

如歌彻婉言 2024-12-08 11:14:08

可能你的问题是你忘记编译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?

自由如风 2024-12-08 11:14:08

首先我会问一个显而易见的问题;您是否在Map.cpp#include编辑了Map.h

我还要指出,Map.h 可能根本不需要包含 Matrix.h。如果您在 Map.h 中对 Matrix 的唯一引用是指针/引用参数,那么您可以简单地向前声明该类:

// forward declare some currently unknown type "Matrix"
class Matrix;

class Map
{
public: 
    void SomeOperation( const Matrix& matrix );
};

只要参数是指针或引用类型那么编译器不需要知道矩阵的大小,因此一切正常。如果可以的话,这比在 .h 文件中包含更多标头更好。然后,您将在 Map.cpp 文件中包含 Matrix.hMap.h

First I'll ask the obvious; have you #includeed Map.h in Map.cpp?

I would also point out that Map.h may not need to include Matrix.h at all. If your only references to a Matrix in Map.h are pointer/reference arguments then you can simply forward declare the class:

// forward declare some currently unknown type "Matrix"
class Matrix;

class Map
{
public: 
    void SomeOperation( const Matrix& matrix );
};

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 both Matrix.h and Map.h in your Map.cpp file.

我最亲爱的 2024-12-08 11:14:08

您可能希望在编译中包含两个 .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?

谁许谁一生繁华 2024-12-08 11:14:08

简单的规则:-

  1. 在头文件中使用防护,例如:-

    ifndef MATRIX_H

定义 MATRIX_H

...
...头文件的东西
...

endif

  1. 在头文件中包含所有类定义/其他声明。相关 .cpp 文件中的定义。

  2. 始终尽量避免在一个头文件中包含另一个头文件。
    大多数情况下,外部声明就可以了。
    链接会自动解析引用。

  3. 可以包含 .cpp 文件中的头文件。但在这里,大多数情况下,事情只需要外部声明即可。

Simple rules:-

  1. Use guards in header files like:-

    ifndef MATRIX_H

define MATRIX_H

...
... header file things
...

endif

  1. Have all class definitions/other declarations in header file. Definitions in related .cpp files.

  2. 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.

  3. 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.

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