简体   繁体   English

定义模板化类函数时出错

[英]Errors defining templated class functions

I'm working on a template assignment for class and my code is giving me errors at these specific lines. 我正在为类分配模板,我的代码在这些特定行给我错误。 "'T' is not a valid template type argument for parameter 'T'" “'T'不是参数'T'的有效模板类型参数”

It's real confusing for me because I've never worked with namespaces before. 这对我来说真的很混乱,因为我以前从未使用过名称空间。 Nor have I worked with an .inl. 我也没有使用.inl。 Could someone help correct my code? 有人可以帮助纠正我的代码吗? I've inserted comments at where the compiler is giving me errors, they're the functions that return a Node*. 我在编译器给我错误的地方插入了注释,它们是返回Node *的函数。 My code is not finished either. 我的代码也没有完成。

BST.hpp BST文件

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

In this line: 在这一行:

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

There is no Node class at global scope for you to return. 全局范围内没有Node类供您返回。 That's the only thing Node can refer to so far, since the compiler is still not inside BST<T> 's scope to find the internal Node . 到目前为止,这是Node唯一可以引用的内容,因为编译器仍不在BST<T>的范围内以查找内部Node There are two ways to fix this: 有两种方法可以解决此问题:

  1. Fully qualify the Node (works since C++98): 完全限定Node (自C ++ 98起可以使用):

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

    This fully qualifies the scope where Node is. 这完全证明了Node所在的范围。

  2. Use a trailing return type (my personal preference, C++11 and onward): 使用尾随返回类型(我个人的喜好,C ++ 11及更高版本):

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

    This delays the lookup of Node until it may begin inside the scope of the class. 这会延迟Node的查找,直到它可以在类的范围内开始。

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

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