Ich schreibe eine Vector3D-Klasse, die eine statische Methode für eine VectorMath-Klasse aufruft, um eine Berechnung durchzuführen. Wenn ich kompilieren, bekomme ich diese:C++ - Linker Probleme mit statischen Methode
bash-3.1$ g++ VectorMath.cpp Vector3D.cpp /tmp/cc5cAPia.o: In function `main': Vector3D.cpp:(.text+0x4f7): undefined reference to 'VectorMath::norm(Vector3D*)' collect2: ld returned 1 exit status
Der Code:
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
}
Warum kann die VectorMath::norm
Methode nicht der Linker finden? Auf den ersten Blick würde ich denken, dass ich brauchen würde Norm wie folgt zu erklären:
Vector3D* VectorMath::norm(Vector3D* vector) {
aber das hilft auch nicht ...
Dies: "aber das hilft auch nicht ..." ist nicht genug Information. Was bedeutet das? Gleiche Fehler, verschiedene Fehler? – GManNickG