Possible Duplicate:
Why can templates only be implemented in the header file?
I'm working on a Matrix class, and I have a method SetMatrix that I use to initialize the array with standard c++ two dimensional arrays.
template <int width, int height>
static void SetMatrix(Matrix& matrix, double array[][width]) {
if (matrix.height_ != height || matrix.width_ != width) {
fprintf(stderr, "Incorrect matrix size\n");
return;
}
for (int y = 0; y < height; ++y)
for (int x = 0; x < width; ++x)
matrix.matrix_[y][x] = array[y][x];
}
and the method works well, but when I try to move it from the header file to the .cpp implementation file, I get a linker error.
Matrix.cpp:
template <int width, int height>
void Matrix::SetMatrix(Matrix& matrix, double array[][width]) {
if (matrix.height_ != height || matrix.width_ != width) {
fprintf(stderr, "Incorrect matrix size\n");
return;
}
for (int y = 0; y < height; ++y)
for (int x = 0; x < width; ++x)
matrix.matrix_[y][x] = array[y][x];
}
g++ output:
g++ -Wall -Wextra main.cpp Matrix.cpp -o matrix
/tmp/ccMkObBs.o: In function `main':
main.cpp:(.text+0x81): undefined reference to `void Matrix::SetMatrix<2, 3>(Matrix&, double (*) [2])'
collect2: error: ld returned 1 exit status
I was wondering how to move it to the .cpp file, or if I'm just out of luck, or if there's a more elegant way to do it? Most of the resources I've found were dealing with static method of template classes, not a static template method of a non-template class. Here's my calling code, too, if it will help
int main() {
double input[3][2] = {
{1, 2},
{3, 4},
{5, 6}
};
Matrix matrix(2, 3);
Matrix::SetMatrix<2, 3>(matrix, input);
matrix.Print();
}