Templates & Copy Constructors

Hey all. Having some trouble with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef Matrix_H_INCLUDED
#define Matrix_H_INCLUDED

template <class T>
class Matrix{
...
    Matrix(const Matrix<T>& mat); //Copy constructor
...
};

Matrix<double> solve(Matrix<double> a, Matrix<double> b) throw(int);

#endif

...

Matrix<double> a(4,4);
...
Matrix<double> b(4,1);
...
solve(a,b); ****************** Error occurs on this line.


Error: undefined reference to `Matrix<double>::Matrix(Matrix<double> const&)'

I'm not sure why I am getting this error. I've defined the copy constructor, why can't it find it?

Thanks.

Nick.
Last edited on
Ok it was because I had the implementation (Matrix.cpp) outside of the declaration (Matrix.h). Now I have the implementation inside Matrix.h and it compiles. Should the implementation be inside the:

1
2
3
4
#ifndef Matrix_H_INCLUDED
#define Matrix_H_INCLUDED
...
#endif 


Or should I not have these statement at all? I'm not really sure with templates.

Thanks.

Nick.
Last edited on
Header files should always have include guards.
Topic archived. No new replies allowed.