C ++ linker issues with static method

I am writing a Vector3D class that calls a static method in the VectorMath class to perform a calculation. When I compile, I get the following:

  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

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 } 

Why can't the linker find the VectorMath::norm method? At first glance, I would think that I need to declare such a norm:

 Vector3D* VectorMath::norm(Vector3D* vector) { 

but it doesn’t help ...

+4
source share
3 answers

You are missing this:

 //VectorMath.cpp #include "VectorMath.h" | V - here Vector3D* VectorMath::norm(Vector3D* vector) { ... } 

The norm function is part of VectorMath:: . Without it, you just have a free function.


This is more about your design, but why are you using pointers to everything? This is much cleaner:

 class VectorMath { public: static Vector3D norm(const Vector3D&); }; 

Take the links, you are in C ++, so do not write C code. What happens when I call it?

 VectorMath::norm(0); // null 

Is it either a failure, you have to put a check, and in this case, what should it return? All this is cleared using links.

Also, why not just make these members of the Vector3D class?

 Vector3D* v = new Vector3D(x, y, z); v->norm(); // normalize would be better, in my opinion 

Finally, lay out the stack. Your code has a memory leak right now:

 int main(void) { Vector3D* v = new Vector3D(x, y, z); Vector3D* normVector = VectorMath::norm(v); // delete v; // ^ you're not deleting it! } 

Change it and use RAII :

 int main(void) { Vector3D v(x, y, z); Vector3D* normVector = VectorMath::norm(v); // delete v; // ^ you're not deleting it! } 

And by creating norm a member function, you get very clean code:

 int main(void) { Vector3D v(x, y, z); Vector3D normVector(v.norm()); } 

No pointers, no leaks, everything is sexy.

+14
source

You have not defined the Vector3D::norm method in VectorMath.cpp . Instead, you defined a global function called norm . What you need to do is define the method name in the definition:

 Vector3D* Vector3D::norm(Vector3D* vector) 
+4
source
 Vector3D* VectorMath::norm(Vector3D* vector) { // can't be found by linker // do vector calculations return new Vector3D(xHead, yHead, zHead, xTail, yTail, zTail); } 
0
source

All Articles