繁体   English   中英

模板类如何继承另一个模板类?

[英]How does a template class inherit another template class?

我有一个“SquareMatrix”模板类,它继承了“Matrix”模板类,如下所示:

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

我注释掉了所有其他行,文件内容与上面完全相同。

我收到这些错误消息(Visual Studio 2010):

第49行:智能感知:期待一个声明
第52行:IntelliSense:预期声明
第53行:智能感知:预期声明
第54行:错误C2039:'GetDeterminant':不是'SquareMatrix'的成员
第54行:IntelliSense:预期声明

那么,继承模板类的正确方法是什么?
这段代码有什么问题?

“矩阵”类:

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不是编译器,它是一个完成工具。 它不会编译整个程序,因此它无法真正理解所有内容。 它还可以在您修改代码时动态运行。 因此,当您使用模板,多个包含不同的定义,更改具有依赖性的内容等时,它可能会丢失...

请用编译器 编译代码,它起作用。

你没有问题! 除了您正在使用的编译器之外。

你展示的代码看起来非常好。 必须有其他原因导致问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM