problem with class templates

I recreated my Matrix class using templates. I use class template T so I can parameterize the elements.

I keep getting the following errors:
error C2955: 'Matrix' : use of class template requires template argument list
error C2512: 'Matrix' : no appropriate default constructor available

In my template examples from my book, all the constructors do have a template argument list, but in my case it would be illogical to make the type of rows and cols dynamic since this will always be represented by 'int'.

Here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
template<class T>
class Matrix
{
public:
	Matrix(int rows, int cols);

	void setElement(int row, int column, T value) { m_elements[row][column] = value; }
	T getElement(int row, int column) const { return m_elements[row][column]; }
	int getRows() const { return m_rows; }
	int getColumns() const { return m_cols; }

private:
	T ** m_elements;
	int m_rows;
	int m_cols;
};

template<class T>
Matrix::Matrix(int rows, int cols)
{
		m_rows = rows;
		m_cols = columns;
		
		m_elements = new T * [m_rows];
		for(int i=0; i<m_rows; i++)
			m_elements[i] = new T[m_cols];
}


And test program:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;
#include "Matrix.h"

int main()
{
	Matrix <int> * m = new Matrix(2, 2);
	m->setElement(0,0,1);
	
	return 0;
}
18
19
template<class T>
Matrix<T>/* <- added the template argument to Matrix*/::Matrix(int rows, int cols)

I adjusted the code you mentioned. but now I still get C2955 when a create a Matrix instance in the main function.

Also on that same line I know get error C2514: 'Matrix' : class has no constructors

Though, when I create a Matrix class like this:
 
Matrix <int> m(2, 2);


It compiles perfectly.
Is Matrix::Matrix defined in the header file?
Matrix<int> *m = new Matrix<int>(2, 2);
you're right jsmith. That works just fine.
Thank you
Topic archived. No new replies allowed.