【发布时间】:2021-08-14 12:31:35
【问题描述】:
我使用 typedef 和结构创建了一个新类型。我想将该类型导出为模块。
我有以下cpp头文件:
//header.h
#ifndef VECTOR_H
#define VECTOR_H
typedef struct {
float x;
float y;
float z;
} Vector;
#endif
#ifndef PLANE_H
#define PLANE_H
class Plane {
public:
//this works
Vector n;
//this does not work
Plane(Vector P1, Vector P2, Vector);
};
#endif
这是模块文件:
//module.cpp
#include header.h
typedef struct {
float x;
float y;
float z;
} Vector;
class Plane {
public:
Vector n;
Plane(Vector P1, Vector P2, Vector P3) {...}
};
在这个文件中,我创建了一个调用构造函数的类Plane的对象:
//main.cpp
#include header.h
float distance = 10;
int main() {
Vector P1 = { 0, 0, 11.5 };
Vector P2 = { 0, distance, 10 };
Vector P3 = { distance, distance, 5 };
Plane E(P1, P2, P3);
return 0;
}
这会引发以下错误:
undefined reference to `Plane::Plane(Vector, Vector, Vector)'
是什么导致了这个错误,我该如何解决?
我使用如下命令编译:
g++ main.cpp header.h
【问题讨论】:
-
@Lala5th 我已经包含了编译命令
-
@Lala5th 当我使用 g++ main.cpp calculate.cpp 构建它时,我收到错误
previous definition of 'class Plane'
标签: c++ oop g++ header-files