C++有两个类的导入循环?
我确定以前有人问过这个问题,但我似乎找不到它。
我有两个类,Vector
和 Point
。
这些文件是这样的(有点重复):
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
和point.h
看起来基本相同,除了在定义中将 vector
与 point
交换。
我这样包含它们:
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
#include "point.h"
和(大概)point.cpp需要#include "vector.h"
。Vector
的构造函数按值获取Point
,因此必须知道其大小;更改Vector
的构造函数以通过const
引用获取Point
,并且前向声明仍然足够。#pragma Once
(如果您不介意不 100% 可移植的话)。编辑(响应OP的编辑):
您的声明和定义现在不匹配 - 即,您的定义是正确的,但您的声明需要
Point const&
而不仅仅是点常量
。#include "point.h"
and (presumably) point.cpp needs#include "vector.h"
.Vector
's constructor is taking aPoint
by value, its size must be known; changeVector
's constructor to take thePoint
byconst
reference instead and a forward declaration will remain sufficient.#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 justPoint const
.