1

So I am attempting to convert an int based binary search tree I created earlier into a templated binary search tree. I am struggling with how to type out my .cpp file.

In my .h file I have:

template <typename T>
class BST
{
private:        
    struct node
    {
        T m_data;
        node* m_left;
        node* m_right;
    };

    node* m_root;
    void AddLeafPrivate(const T& x, node* Ptr);
    void PrintInOrderPrivate(node* Ptr);
    node* ReturnNodePrivate(const T& x, node* Ptr);
    T FindSmallestPrivate(node* Ptr);
    void RemoveNodePrivate(const T& x, node* parent);
    void RemoveRootMatch();
    void RemoveMatch(node* parent, node* match, bool left);
    node* CreateLeaf(const T& x);
    node* ReturnNode(const T& x);
    void RemoveSubtree(node* Ptr);
public:
    BST();
    ~BST();
    void AddLeaf(const T& x);
    void PrintInOrder();
    T ReturnRootKey();
    void PrintChildren(const T& x);
    T FindSmallest();
    void RemoveNode(const T& x);
};

In my .cpp file I am getting errors with the following functions:

template <typename T>
template BST<T> :: node* BST<T>::ReturnNode(const T& x)
{
  return ReturnNodePrivate(x, m_root);
}

template <typename T>
template BST<T> :: node* BST<T> :: ReturnNodePrivate(const T& x, node* Ptr)
{
if(Ptr != NULL)
{
    if (Ptr -> m_data == x)
    {
        return Ptr;
    }
    else 
    {
        if (x < Ptr -> m_data)
        {
            return ReturnNodePrivate(x, Ptr -> m_left);
        }
        else 
        {
            return ReturnNodePrivate(x, Ptr -> m_right);
        }       
    }
}
else
{
    return NULL;
}
}

I am getting the same 4 errors for each function and I am confused what I am doing wrong.

  • error: expected '<' before 'BST' for template BST<T>
  • error: 'T' was not declared in this scope
  • error: template argument 1 is invalid
  • error: expected initializer before '*' token

Any suggestions on what I am doing wrong?

greatwolf
  • 20,287
  • 13
  • 71
  • 105
John
  • 73
  • 1
  • 1
  • 6
  • what are the lines of the errors? – ThomasMcLeod Dec 16 '14 at 20:31
  • "In my .cpp file..." in all but the rarest of circumstances you can stop right there. Regarding your error. remove the word `template` before `BST`. It doesn't belong there. Use `typename`. – WhozCraig Dec 16 '14 at 20:31
  • @Borgleader No, it doesn't seem to be this problem. The common problems described in the linked question are related to linking not compiling. – BartoszKP Dec 16 '14 at 20:31
  • You definitely need to replace the bare `template` on lines 2 and 8 of the cpp with `typename` because `BST::node` is a dependent type. – JHumphrey Dec 16 '14 at 20:37
  • @0x499602D2 It's not a duplicate of the linked question... – BartoszKP Dec 16 '14 at 20:48
  • Thanks everyone I felt pretty silly after seeing the error. I am now having to deal with some undefined reference errors that I am going to try to solve, I will be back if I cant figure them out. Thanks again for the answers! – John Dec 16 '14 at 20:51
  • @Justin For that matter, the supposed duplicate has the answer for "undefined reference errors" :) – BartoszKP Dec 16 '14 at 21:49

1 Answers1

4

It seems you've mistakenly typed template twice, instead of typename. For example, it should be:

template <typename T>
typename BST<T>:: node* BST<T>::ReturnNode(const T& x)

instead of:

template <typename T>
template BST<T>:: node* BST<T>::ReturnNode(const T& x)
BartoszKP
  • 34,786
  • 15
  • 102
  • 130