How does a template class inherit another template class?
Posted
by
hkBattousai
on Stack Overflow
See other posts from Stack Overflow
or by hkBattousai
Published on 2010-12-26T14:50:14Z
Indexed on
2010/12/26
14:53 UTC
Read the original article
Hit count: 671
I have a "SquareMatrix" template class which inherits "Matrix" template class, like below:
SquareMatrix.h:
#ifndef SQUAREMATRIX_H
#define SQUAREMATRIX_H
#include "Matrix.h"
template <class T> class SquareMatrix : public Matrix<T>
{
public:
T GetDeterminant();
};
template <class T> // line 49
T SquareMatrix<T>::GetDeterminant()
{
T t = 0; // Error: Identifier "T" is undefined // line 52
return t; // Error: Expected a declaration // line 53
} // Error: Expected a declaration // line 54
#endif
I commented out all other lines, the files contents are exactly as above.
I receive these error messages:
LINE 49: IntelliSense: expected a declaration
LINE 52: IntelliSense: expected a declaration
LINE 53: IntelliSense: expected a declaration
LINE 54: error C2039: 'GetDeterminant' : is not a member of 'SquareMatrix'
LINE 54: IntelliSense: expected a declaration
So, what is the correct way of inheriting a template class?
And what is wrong with this code?
The "Matrix" class:
template <class T> class Matrix
{
public:
Matrix(uint64_t unNumRows = 0, uint64_t unNumCols = 0);
void GetDimensions(uint64_t & unNumRows, uint64_t & unNumCols) const;
std::pair<uint64_t, uint64_t> GetDimensions() const;
void SetDimensions(uint64_t unNumRows, uint64_t unNumCols);
void SetDimensions(std::pair<uint64_t, uint64_t> Dimensions);
uint64_t GetRowSize();
uint64_t GetColSize();
void SetElement(T dbElement, uint64_t unRow, uint64_t unCol);
T & GetElement(uint64_t unRow, uint64_t unCol);
//Matrix operator=(const Matrix & rhs); // Compiler generate this automatically
Matrix operator+(const Matrix & rhs) const;
Matrix operator-(const Matrix & rhs) const;
Matrix operator*(const Matrix & rhs) const;
Matrix & operator+=(const Matrix & rhs);
Matrix & operator-=(const Matrix & rhs);
Matrix & operator*=(const Matrix & rhs);
T& operator()(uint64_t unRow, uint64_t unCol);
const T& operator()(uint64_t unRow, uint64_t unCol) const;
static Matrix Transpose (const Matrix & matrix);
static Matrix Multiply (const Matrix & LeftMatrix, const Matrix & RightMatrix);
static Matrix Add (const Matrix & LeftMatrix, const Matrix & RightMatrix);
static Matrix Subtract (const Matrix & LeftMatrix, const Matrix & RightMatrix);
static Matrix Negate (const Matrix & matrix);
// TO DO:
static bool IsNull(const Matrix & matrix);
static bool IsSquare(const Matrix & matrix);
static bool IsFullRowRank(const Matrix & matrix);
static bool IsFullColRank(const Matrix & matrix);
// TO DO:
static uint64_t GetRowRank(const Matrix & matrix);
static uint64_t GetColRank(const Matrix & matrix);
protected:
std::vector<T> TheMatrix;
uint64_t m_unRowSize;
uint64_t m_unColSize;
bool DoesElementExist(uint64_t unRow, uint64_t unCol);
};
© Stack Overflow or respective owner