定义模板化类函数的错误

时间:2017-12-05 08:11:42

标签: c++ oop templates

我正在为类的模板分配工作,我的代码在这些特定的行上给了我错误。 "' T'不是参数''"

的有效模板类型参数

这对我来说真的很困惑,因为我之前从未使用过命名空间。我也没有和.inl一起工作过。有人可以帮助纠正我的代码吗?我已经在编译器给我错误的地方插入了注释,它们是返回Node *的函数。我的代码也没有完成。

BST.hpp

#ifndef BST_HPP
#define BST_HPP

namespace Tree{
    template<class T>
    class BST {
    public:
        BST();//default constructor
        BST(T rootkey);//constructor with 1 parameter for root key
        ~BST();//destructor
        void insert(T value); //function named insert with 1 parameter(key value to insert)
        Node* remove(class Node* troot,T value);//function named remove with 1 parameter (key value to remove)
        Node* min(class Node* mini);
    private:
        class Node {
        public:
            Node();//default constructor
            Node(T k);//constructor with 1 parameter (key value)
            ~Node(); //destructor
            Node* getP(); //parent accessor
            Node* getL();//left accessor
            Node* getR();//right accessor
            parent mutator
            left mutator
            right mutator
            operator <
            operator ==
            operator !=
            operator <<
        private:
            T key;
            Node *parent;
            Node *left;
            Node *right;
        };
        Node *root;
        void destroy(Node* r);


    };



};

#include "BST.inl"
#endif

BST.inl

#include "BST.hpp"
template<class T>
inline Tree::BST<T>::BST(T rootkey)
{
    root = new Node(rootkey);
    root->setP(0);
}

template<class T>
inline Tree::BST<T>::BST()
{
    root = new Node();
}


template<class T>
inline Tree::BST<T>::~BST()
{
    delete root;
}

template<class T>
inline void Tree::BST<T>::insert(T value)
{
    Node *temp = root;
    Node *prev;
    do
    {
        if (value < temp->getK())
        {
            prev = temp;
            temp = temp->getL();
        }
        else if (value >= temp->getK())
        {
            prev = temp;
            temp = temp->getR();
        }
    } while (temp != NULL);
    temp = new Node(value);
    temp->setP(prev);
    if (temp->getK() > prev->getK())
    {
        prev->setR(temp);
    }
    else
    {
        prev->setL(temp);
    }
}

template<class T>
inline Node * Tree::BST<T>::remove(Node *troot, T value)//ERROR
{ 
    Node *temp;
    if (troot == NULL)
    {
        return troot;
    }
    else if (value > troot->getK())
    {
        return remove(troot->getR(), value)
    }
    else if (value < troot->getK())
    {
        return remove(troot->getL(), value)
    }
    else
    {
        if (troot->getL() == NULL && troot->getR() == NULL)
        {
            delete troot;
            troot = NULL;
            return troot;
        }
        else if (troot->getR() == NULL)
        {
            temp = troot;
            troot = troot->getL();
            return troot;
        }
        else if (troot->getL() == NULL)
        {
            temp = troot;
            troot = troot->getR();
            return troot;
        }
        else
        {
            temp = min(troot->getR());
            troot->setK(temp->getK());
            troot->getR() = remove(troot->getR, temp->getK());
        }
    }
    return troot;
}

template<class T>
inline Node * Tree::BST<T>::min(Node * mini)//ERROR
{
    if (mini->getL() != NULL)
    {
        mini = mini->getL();
    }
    else
        return mini;
}

template<class T>
inline Tree::BST<T>::Node::Node()
{
    key = 0;
}

template<class T>
inline Tree::BST<T>::Node::Node(T k)
{
    key = k;
}

template<class T>
inline Tree::BST<T>::Node::~Node()
{
    delete getL();
    delete getR();
}

template<class T>
inline Node * Tree::BST<T>::Node::getP()//ERROR
{
    return parent;
}

template<class T>
inline Node * Tree::BST<T>::Node::getL()//ERROR
{
    return left;
}

1 个答案:

答案 0 :(得分:3)

在这一行:

template<class T>
inline Node * Tree::BST<T>::Node::getP() {

全球范围内没有Node类供您返回。这是Node到目前为止唯一可以引用的内容,因为编译器仍然不在BST<T>的范围内来查找内部Node。有两种方法可以解决这个问题:

  1. 完全限定Node(自C ++ 98起作用):

    template<class T>
    inline Tree::BST<T>::Node * Tree::BST<T>::Node::getP() {
    

    这完全符合Node所在的范围。

  2. 使用尾随返回类型(我的个人偏好,C ++ 11及以后版本):

    template<class T>
    inline auto Tree::BST<T>::Node::getP() -> Node* {
    

    这延迟了Node的查找,直到它可以在班级范围内开始。

相关问题