cout
cout

Reputation: 25

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'"

It's real confusing for me because I've never worked with namespaces before. Nor have I worked with an .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*. My code is not finished either.

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

Upvotes: 0

Views: 52

Answers (1)

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. 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. There are two ways to fix this:

  1. Fully qualify the Node (works since C++98):

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

    This fully qualifies the scope where Node is.

  2. Use a trailing return type (my personal preference, C++11 and onward):

    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.

Upvotes: 3

Related Questions