VectorMath クラスの静的メソッドを呼び出して計算を実行する Vector3D クラスを作成しています。コンパイルすると、次のようになります。
bash-3.1$ g++ VectorMath.cpp Vector3D.cpp /tmp/cc5cAPia.o: 関数「main」内: Vector3D.cpp:(.text+0x4f7): 'VectorMath::norm(Vector3D*)' への未定義の参照 collect2: ld が 1 つの終了ステータスを返しました
コード:
VectorMath.h:
#ifndef VECTOR3D_H
#include "Vector3D.h"
#endif
class VectorMath {
public:
static Vector3D* calculatePerpendicularVector(Vector3D*, Vector3D*);
static Vector3D* norm(Vector3D*);
static double length(Vector3D*);
};
VectorMath.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
Vector3D.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) {
しかし、それも役に立ちません...