What's wrong with this class template?

Hello,

I created a floating-point 3d vector class, which I decided to make into a template so I could choose between float, double, etc.

I will not post the entire code, but here's the class definition (from the header file) and the two forms of constructor (from the separate .cpp file).

My compiler generates the following error message, when I try to instantiate a vector3 of doubles:
[Linker error] undefined reference to `vector3<double>::vector3(double, double, double)'

From vector3.h:
1
2
3
4
5
6
7
8
9
10
11
12
template <class T>
class vector3 {
      
      private:
       T v[3];
       
      public:
             
       vector3();
       vector3(T x, T y, T z);
       ~vector3();
}


From vector3.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "vector3.h"

template <class T>
vector3<T>::vector3() {

    v[0] = 0.0;
    v[1] = 0.0;
    v[2] = 0.0;
                     
}

template <class T>
vector3<T>::vector3(T x, T y, T z) {
    
    v[0] = x;
    v[1] = y;
    v[2] = z;

}


Could I be missing something quite obvious?
Thanks for any help.
I think it is because of how templates work; you cannot separate the source code from the definition because the classes are generated as necessary.
Yep. That worked. I put all the function definitions in the header file, and that took care of it. Although, I don't like that because it goes against the way everything is already organized...
Topic archived. No new replies allowed.