I started off trying to do this for rectangular matrices but ran into too many problems, so I backed up to square matrices and moved the dimension into the template specification. But I still can't get the main constructor to work.

First, here's my incipient header file:

Code:

#ifndef MATRIX_H
#define MATRIX_H
template<typename T, int n> // is the Matrix height/width
class Matrix
{
private:
T** content; // 2-dimensional array of T objects
public:
// no default constructor because we don't know what T is
// construct a Matrix from an array
Matrix(const T arr[][n]);
// copy constructor
Matrix(const Matrix& m);
// destructor
~Matrix();
// basic matrix operations
// addition
Matrix<T, n> operator+(const Matrix<T, n>& m) const;
// other methods
void show();
};
#endif

Now here's the relevant constructor definition:

Code:

template<typename T, int n>
Matrix<T, n>::Matrix(const T arr[][n])
{
if (n > 0) // validate input at least a little
{
// copy contents (!)
// allocate memory
T** cont_arr = new T*[n];
for (int i = 0; i < n; ++i)
{
cont_arr[i] = new T[n];
}
// set values
for (int i = 0; i < n; ++i) // rows
{
for (int j = 0; j < n; ++j) // columns
{
cont_arr[i][j] = arr[i][j];
}
}
content = cont_arr;
}
else // bad value for n
{
std::cout << "Error! Matrix dimension must be > 0.\n";
content = 0;
}
}

Now here's main with the problematic part commented out:

Code:

#include <iostream>
#include "matrix.h"
int main()
{
int arr1[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
int arr2[3][3] = {{10,9,8},{20,19,18}, {30,29,28}};
/*
Matrix<int, 3> matrix1(arr1);
Matrix<int, 3> matrix2(arr2);
// */

It compiles in this form, but as soon as I uncomment the creation of the actual Matrix objects, I get a long drawn-out error message:

error LNK2001: unresolved external symbol "public: __thiscall Matrix<int,3>::~Matrix<int,3>(void)" (??1?$Matrix@H$02@@QAE@XZ)

I'm not understanding the problem here... probably something really stupid