How does a template class inherit from another template class?

I have a SquareMatrix template template that inherits the Matrix template class, as shown 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 the other lines, the contents of the files are exactly the same as above.

I get these error messages (Visual Studio 2010):

LINE 49: IntelliSense: expected declaration
LINE 52: IntelliSense: expected declaration
LINE 53: IntelliSense: expected declaration
LINE 54: error C2039: "GetDeterminant": not a member of SquareMatrix

LINE 54: IntelliSense: expected declaration

So what is the correct way to inherit a template class?
And what is wrong with this code?

Class "Matrix":

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);
};
+3
3

IntelliSense , . , . " " . , , -, , ..

, .

+4

! , , , .

+3

, , . - , .

+1

Source: https://habr.com/ru/post/1782175/


All Articles