Ble*_*der 1 c++ import compiler-errors class
我确定之前已经问过这个问题,但我似乎无法找到它.
我有两节课,Vector和Point.
这些文件是这样的(有点重复):
vector.h:
#include <math.h>
#include <stdlib.h>
class Vector {
friend class Point;
public:
...
Vector(Point); // Line 16
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
point.cpp并且point.h看起来几乎相同,除了你换vector用point的定义.
我把它们包括在内:
#include "structures/vector.cpp"
#include "structures/point.cpp"
Run Code Online (Sandbox Code Playgroud)
当我编译时,我收到此错误:
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
Run Code Online (Sandbox Code Playgroud)
我觉得这个错误说Point未声明的是,但是当我宣布它的内部vector.h导入point.cpp,我得到一个巨大的错误一堆.
有人能解释一下这个问题吗?
谢谢!
在应用@ ildjarn的建议后,这些错误就消失了,我只剩下这个错误:
structures/vector.h:16:18: error: expected ‘)’ before ‘const’
Run Code Online (Sandbox Code Playgroud)
和行:
Vector(Point const);
Run Code Online (Sandbox Code Playgroud)
我在.cpp文件中定义它:
Vector::Vector(Point const &point) {
Run Code Online (Sandbox Code Playgroud)
#include "point.h"和(推测)point.cpp需要#include "vector.h".Vector构造函数是Point按值计算的,所以它的大小必须是已知的; 变化Vector的构造采取Point的const参考,而不是和一个向前声明将保持充裕.#pragma once如果你不介意不是100%可移植).编辑(响应OP的编辑):
您的声明和定义现在不匹配 - 即您的定义是正确的,但您的声明需要Point const&而不仅仅是Point const.