开发者

How does a template class inherit another template class?

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;   开发者_Go百科    // 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 (Visual Studio 2010):

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


IntelliSense is not a compiler, it's a completion tool. It doesn't compile the whole program, hence it can't really understand everything. It also works on the fly as you modify your code. Therefore it may get lost when you use templates, multiple includes with different defines, change something that has dependencies, etc...

Please compile the code with a compiler, it will work.


YOU HAVE NO PROBLEM! Except perhaps for the compiler that you're using.


The code you showed looks perfectly fine. There must be something else causing the problems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜