Click to See Complete Forum and Search --> : Trouble with templates... (C++)


Handyman
07-07-2007, 12:31 PM
Greetings,

I'm attempting to create a template matrix class for a series of applications I plan to work on, however, I'm having some trouble just getting past setting up my constructor/destructor. Here are my header and cpp files:

mcs_matrix.h

#ifndef MCS_MATRIX
#define MCS_MATRIX

namespace MCS_MATRIX_LIB
{
template < typename ELEMENT_TYPE >

class Matrix
{
public:

// Constructors
Matrix( unsigned int inRows , unsigned int inCols );

// Destructor
~Matrix();

protected:

// Matrix Dimensions
unsigned int Rows;
unsigned int Cols;

// Matrix Elements
ELEMENT_TYPE *Element;
};
}

#endif


mcs_matrix.cpp

#include "mcs_matrix.h"

namespace MCS_MATRIX_LIB
{
template < typename ELEMENT_TYPE >

Matrix<ELEMENT_TYPE>::Matrix(unsigned int inRows, unsigned int inCols)
{
// Validate Arguments
// TODO?

// Set Rows and Cols members
Rows = inRows;
Cols = inCols;

// Allocate matrix storage
Element = new ELEMENT_TYPE[Rows*Cols];
}

Matrix<ELEMENT_TYPE>::~Matrix()
{
// Destroy Element memory
delete [] Element;
}
}


I am compiling in Visual Studio 2005, with the project creating a static library (*.lib) file (since I plan to use this class in several applications). When building I get the following errors:


Error 1 error C2065: 'ELEMENT_TYPE' : undeclared identifier c:\mcs_projects\mcs_matrix_lib\mcs_matrix_lib\mcs_matrix.cpp 32
Error 2 error C2955: 'MCS_MATRIX_LIB::Matrix' : use of class template requires template argument list c:\mcs_projects\mcs_matrix_lib\mcs_matrix_lib\mcs_matrix.cpp 32
Error 3 error C2509: '{dtor}' : member function not declared in 'MCS_MATRIX_LIB::Matrix' c:\mcs_projects\mcs_matrix_lib\mcs_matrix_lib\mcs_matrix.cpp 33


So naturally I remove the "<ELEMENT_TYPE>" from the destructor implementation so that I get the code below inside mcs_matrix.cpp:


Matrix::~Matrix()
{
// Destroy Element memory
delete [] Element;
}


But then I get errors that lead to want to put the "<ELEMENT_TYPE>" back in. Here are those errors:


Error 1 error C2955: 'MCS_MATRIX_LIB::Matrix' : use of class template requires template argument list c:\mcs_projects\mcs_matrix_lib\mcs_matrix_lib\mcs_matrix.cpp 32
Error 2 error C2955: 'MCS_MATRIX_LIB::Matrix' : use of class template requires template argument list c:\mcs_projects\mcs_matrix_lib\mcs_matrix_lib\mcs_matrix.cpp 32
Error 3 error C2509: '{dtor}' : member function not declared in 'MCS_MATRIX_LIB::Matrix' c:\mcs_projects\mcs_matrix_lib\mcs_matrix_lib\mcs_matrix.cpp 33


This is my first attempt at a template class... and I'm stuck in the mud :( Deitel has failed me for the first time :eek: Anyone see something obviously wrong with my class so far?

Handyman
07-07-2007, 12:35 PM
As usual, once I post in a forum for help, I figure out the problem. I just needed to place the "template < typename ELEMENT_TYPE >" above each function implementation. Blast! :mad:


#include "mcs_matrix.h"

namespace MCS_MATRIX_LIB
{
template < typename ELEMENT_TYPE >
Matrix<ELEMENT_TYPE>::Matrix(unsigned int inRows, unsigned int inCols)
{
// Validate Arguments


// Set Rows and Cols members
Rows = inRows;
Cols = inCols;

// Allocate matrix storage
Element = new ELEMENT_TYPE[Rows*Cols];
}

template < typename ELEMENT_TYPE >
Matrix<ELEMENT_TYPE>::~Matrix()
{
// Destroy Element memory
delete [] Element;
}
}