Template Friend Error

Sorry to post so often but I've never really worked with templated friend classes so I'm not sure if I'm doing something wrong or not. According to all the sources I have on hand, the syntax is correct but the compiler's still throwing errors. Anyway, the problem occurs in the remove function of a binary search tree

1
2
3
friend void removeT<>(const T& x,
                      treenode<T>* & root);
virtual void remove(const T& x);


where the implementations are

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
template <class T>
  void remove(const T& x,
              treenode<T>* & root)
  {
    if (root == NULL)
      return;
    else if (x < root->m_data)
      removeT(x, root->m_left);
    else if (x > root->m_data)
      removeT(x, root->m_right);
    else if ((root->m_left != NULL) && (root->m_right != NULL))
    {
      root->m_data = root->m_right->findMin();
      removeT(root->m_data, root->m_right);
    }
    else
    {
      treenode<T>* oldNode = root;
      root = (root->m_left != NULL) ? root->m_left : root->m_right;
      delete oldNode;
    }
    return;
  }

template <class T>
  void MyBSTree<T>::remove(const T& x)
  {
    if (m_root == NULL)
      return;
    else if (x < m_root->m_data)
      removeT(x, m_root->m_left);
    else if (x > m_root->m_data)
      removeT(x, m_root->m_right);
    else if ((m_root->m_left != NULL) && (m_root->m_right != NULL))
    {
      m_root->m_data = m_root->m_right->findMin();
      removeT(m_root->m_data, m_root->m_right);
    }
    else
    {
      treenode<T>* oldNode = m_root;
      m_root = (m_root->m_left != NULL) ? m_root->m_left : m_root->m_right;
      delete oldNode;
      oldNode = NULL;
    }
    return;
  }


The class treenode<T>* is the node of a binary search tree and the class MyBSTree<T> contains a pointer to a treenode<T> for the root of the tree. The compiler is throwing the following errors

1
2
3
4
MyBSTree.h:36:17: error: variable or field ˜removeT" declared void
MyBSTree.h:36:17: error: expected ˜;" at end of member declaration
MyBSTree.h:36:24: error: expected unqualified-id before ˜<" token
I 


as well as a few others. But I'm relatively sure that they all stem from these three or even one of these three. I'm not sure what's going on. Any suggestions?
Last edited on
I managed to get the code working with a different implementation but I'm still curious about this so if anyone has a solution please post.

Thanks
1
2
3
4
5
6
7
8
template < typename T > struct treenode ; // declare treenode<>

template < typename T > void removeT( const T& x, treenode<T>*& root ) ; // declare removeT<>

template < typename T > struct treenode
{
    friend void removeT<> ( const T& x, treenode<T>*& root ) ; // declare friend
};


http://www.parashift.com/c++-faq-lite/template-friends.html
Topic archived. No new replies allowed.