0

I write template class for binary tree:

template <class T>
class Tree {
public:
    Tree():head_(NULL),size_(0){}
    ~Tree();
    bool isEmpty()const {return size_ == 0;};
    bool insert(const T& ele);
    bool remove(const T& ele);
    size_t size() {return size_;} 
public:

    class inorder_iterator 
    {
        inorder_iterator& operator++ ();
    private:
        Node<T>* cur_;
    };
}

What is the defintion for operator++?(I can`t compile using the following)

template <class T>
Tree<T>::inorder_iterator& 
Tree<T>::inorder_iterator::operator++ ()
{
    //....
}
YAKOVM
  • 9,805
  • 31
  • 116
  • 217

1 Answers1

2

With these changes it compiles:

template <class T>
class Node {}; 

template <class T>
class Tree {
    Node<T> head_;
    size_t size_;
public:
    Tree():head_(NULL),size_(0){}
    ~Tree();
    bool isEmpty()const {return size_ == 0;};
    bool insert(const T& ele);
    bool remove(const T& ele);
    size_t size() {return size_;} 
public:

    class inorder_iterator 
    {   
        inorder_iterator& operator++ (); 
    private:
        Node<T>* cur_;
    };  
};

template <class T>
typename Tree<T>::inorder_iterator& 
Tree<T>::inorder_iterator::operator++ ()
{
    //....
}
piokuc
  • 25,594
  • 11
  • 72
  • 102
  • Can you pls explain the need of typename key? – YAKOVM Feb 24 '12 at 17:14
  • This is because of dependent scope of Tree in the return type of the function, here is a good explanation: http://stackoverflow.com/questions/6571381/dependent-scope-and-nested-templates – piokuc Feb 24 '12 at 17:18