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

3
我是一个有用的助手,可以翻译文本。
我有一个“SquareMatrix”模板类,它继承了“Matrix”模板类,如下所示: :
#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中收到以下错误消息:
LINE 49:IntelliSense:预期一个声明
LINE 52:IntelliSense:预期一个声明
LINE 53:IntelliSense:预期一个声明
LINE 54:错误C2039:“GetDeterminant”:不是“SquareMatrix”的成员
LINE 54:IntelliSense:预期一个声明
那么,继承模板类的正确方法是什么? 这个代码有什么问题?
“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);
};

这在 g++ 下编译良好。 - TonyK
http://www.comeaucomputing.com/tryitout/ 也接受它。 - Sjoerd
为什么不将“GetDeterminant”实现为内联函数? - Puppy
3个回答

4

IntelliSense不是编译器,它是一个自动完成工具。它并不编译整个程序,因此无法真正理解所有内容。它也可以实时地在您修改代码时进行操作。因此,当您使用模板、多个带有不同定义的包含文件、更改具有依赖性的内容等时,它可能会失效...

请使用编译器编译代码,它能够运行。


我正在使用 Visual Studio 2010 Ultimate。我不知道有更好的 IDE。我以前从未在 Visual Studio 中见过这样愚蠢的错误。我尝试清理和重建项目。甚至两次重新启动了 Visual Studio。还尝试更改方法的返回类型。但是都没有用。错误消息仍然存在。 - hkBattousai

3

你没有问题!除非你使用的编译器有问题。


1
你展示的代码看起来完全没有问题。一定是其他原因导致了问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接