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++ ()
{
    //....
}
1
  • 2
    Does the third line of the second snippet start with Tree>T> in your source code? Commented Feb 24, 2012 at 16:25

1 Answer 1

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++ ()
{
    //....
}
Sign up to request clarification or add additional context in comments.

2 Comments

Can you pls explain the need of typename key?
This is because of dependent scope of Tree<T> in the return type of the function, here is a good explanation: stackoverflow.com/questions/6571381/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.