C++有两个类的导入循环?

发布于 2024-12-08 19:02:23 字数 1443 浏览 2 评论 0原文

我确定以前有人问过这个问题,但我似乎找不到它。

我有两个类,VectorPoint

这些文件是这样的(有点重复):

vector.h

#include <math.h>
#include <stdlib.h>

class Vector {
  friend class Point;

  public:
    ...

    Vector(Point); // Line 16

vector.cpp

#include <math.h>
#include <stdlib.h>

#include "vector.h"

...

Vector::Vector(Point point) { // Line 29
  x = point.x;
  y = point.y;
  z = point.z;
}

point.cpppoint.h 看起来基本相同,除了在定义中将 vectorpoint 交换。

我这样包含它们:

#include "structures/vector.cpp"
#include "structures/point.cpp"

当我编译时,我收到此错误:

structures/vector.h:16:17: error: field ‘Point’ has incomplete type
structures/vector.cpp:29:15: error: expected constructor, destructor, or type conversion before ‘(’ token

我认为此错误是说 Point 尚未声明,但是当我在 vector.h 内声明它时 通过导入 point.cpp,我得到了一大堆错误。

有人能解释一下这个问题吗?

谢谢你!


在应用@ildjarn的建议后,这些错误消失了,我只剩下了这个错误:

structures/vector.h:16:18: error: expected ‘)’ before ‘const’

还有一行:

Vector(Point const);

我在.cpp文件中这样定义它:

Vector::Vector(Point const &point) {

I'm sure this question has been asked before, but I can't seem to find it.

I have two classes, Vector and Point.

The files are as such (a bit repetitive):

vector.h:

#include <math.h>
#include <stdlib.h>

class Vector {
  friend class Point;

  public:
    ...

    Vector(Point); // Line 16

vector.cpp:

#include <math.h>
#include <stdlib.h>

#include "vector.h"

...

Vector::Vector(Point point) { // Line 29
  x = point.x;
  y = point.y;
  z = point.z;
}

point.cpp and point.h look mostly the same, except you swap vector with point in the definitions.

I include them as such:

#include "structures/vector.cpp"
#include "structures/point.cpp"

When I compile, I get this error:

structures/vector.h:16:17: error: field ‘Point’ has incomplete type
structures/vector.cpp:29:15: error: expected constructor, destructor, or type conversion before ‘(’ token

I think this error is saying that Point hasn't been declared yet, but when I declare it inside of vector.h by importing point.cpp, I get a huge pile of errors.

Could anyone shed some light on this problem?

Thank you!


Upon applying @ildjarn's suggestions, those errors went away and I am left with this single one:

structures/vector.h:16:18: error: expected ‘)’ before ‘const’

And the line:

Vector(Point const);

I define it like so in the .cpp file:

Vector::Vector(Point const &point) {

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

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

发布评论

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

评论(1

眼藏柔 2024-12-15 19:02:23
  1. 您不应该包含 .cpp 文件,而应该包含 .h 文件。
  2. vector.cpp 需要 #include "point.h" 和(大概)point.cpp需要#include "vector.h"
  3. 仅当您不执行任何需要类型大小或接口的操作时,前向声明就足够了。由于 Vector 的构造函数按值获取 Point,因此必须知道其大小;更改 Vector 的构造函数以通过 const 引用获取 Point,并且前向声明仍然足够。
  4. 您的标头需要#include Guards(或#pragma Once(如果您不介意不 100% 可移植的话)。

编辑(响应OP的编辑):

您的声明和定义现在不匹配 - 即,您的定义是正确的,但您的声明需要 Point const& 而不仅仅是 点常量

  1. You shouldn't be including .cpp files, you should be including the .h files.
  2. vector.cpp needs #include "point.h" and (presumably) point.cpp needs #include "vector.h".
  3. A forward declaration is only sufficient if you're not doing anything that requires the type's size or interface. Because Vector's constructor is taking a Point by value, its size must be known; change Vector's constructor to take the Point by const reference instead and a forward declaration will remain sufficient.
  4. Your headers need #include guards (or #pragma once if you don't mind not being 100% portable).

EDIT (in response to OP's edit):

Your declaration and definitions now mismatch -- i.e., your definition is correct but your declaration needs Point const& rather than just Point const.

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