我正在编写一个 Vector3D 类,它调用 VectorMath 类的静态方法来执行计算。当我编译时,我得到这个:
bash-3.1$ g++ VectorMath.cpp Vector3D.cpp /tmp/cc5cAPia.o:在函数“main”中: Vector3D.cpp:(.text+0x4f7): 未定义引用 'VectorMath::norm(Vector3D*)' collect2: ld 返回 1 个退出状态
编码:
矢量数学.h:
#ifndef VECTOR3D_H
#include "Vector3D.h"
#endif
class VectorMath {
public:
static Vector3D* calculatePerpendicularVector(Vector3D*, Vector3D*);
static Vector3D* norm(Vector3D*);
static double length(Vector3D*);
};
矢量数学.cpp
#include "VectorMath.h"
Vector3D* norm(Vector3D* vector) { // can't be found by linker
// do vector calculations
return new Vector3D(xHead, yHead, zHead, xTail, yTail, zTail);
}
// other methods
矢量3D.cpp
#include "Vector3D.h"
#include "VectorMath.h"
// ...
// vector implementation
// ...
int main(void) {
Vector3D* v = new Vector3D(x, y, z);
Vector3D* normVector = VectorMath::norm(v); // error here
}
为什么链接器找不到VectorMath::norm
方法?乍一看,我认为我需要像这样声明规范:
Vector3D* VectorMath::norm(Vector3D* vector) {
但这也无济于事......