Templates & Copy Constructors
Nov 23, 2010 at 2:37am UTC
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 Nov 23, 2010 at 2:40am UTC
Nov 23, 2010 at 3:10am UTC
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 Nov 23, 2010 at 3:10am UTC
Nov 23, 2010 at 4:48am UTC
Header files should always have include guards.
Topic archived. No new replies allowed.