0

I have this class in my .hpp file

template<class T = int>
class Matrix
{
    public:
    Matrix();
}

and I have this Matrix.cpp file

#include "Matrix.hpp"
template<class T>
Matrix<T>::Matrix()
{
    vector<T> vecN(1, 0);
    _matrix.resize(1, vecN);
    _rows = 1;
    _cols = 1;
}

but it won't work, when adding a main

#include "Matrix.hpp"

int main(int argc, char** argv)
{
    Matrix<int> test();
    return 0;
}

i get a very weird error saying

main.cpp:19: undefined reference to Matrix<int>::Matrix(unsigned int, unsigned int)' main.cpp:19:(.text+0x2d): relocation truncated to fit: R_X86_64_PC32 against undefined symbol Matrix<int>::Matrix(unsigned int, unsigned int)

Mumfordwiz
  • 1,483
  • 4
  • 19
  • 31

1 Answers1

3

Template code must be in the header, unless it is for specialisations.

This is because the template is used to generate the actual class when you use it.

Dominique McDonnell
  • 2,510
  • 16
  • 25