Как шаблонный класс наследует другой шаблонный класс? - PullRequest
3 голосов
/ 26 декабря 2010

У меня есть шаблонный класс "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: IntelliSense: ожидается объявление
ЛИНИЯ 52: IntelliSense: ожидается объявление
ЛИНИЯ 53: IntelliSense: ожидается объявление
ЛИНИЯ 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);
};

Ответы [ 3 ]

4 голосов
/ 26 декабря 2010

IntelliSense не является компилятором, это инструмент завершения.Он не компилирует всю программу, следовательно, он не может действительно понять все.Он также работает на лету, когда вы изменяете свой код.Поэтому он может потеряться при использовании шаблонов, множественных включений с разными определениями, изменении чего-либо, имеющего зависимости и т. Д.*, это будет работать.

3 голосов
/ 26 декабря 2010

У тебя нет проблем!За исключением, возможно, компилятора, который вы используете.

1 голос
/ 26 декабря 2010

Код, который вы показали, выглядит отлично.Должно быть что-то еще, вызывающее проблемы.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...