无法实现二叉树的预订迭代器

时间:2012-09-13 13:10:24

标签: c++ templates iterator iterator-facade

template <class T>
struct BinaryTreeNode
{
    T data_;

    BinaryTreeNode* left_;
    BinaryTreeNode* right_;
};


template <class Node>
class TreeItBase : public boost::iterator_facade<
        TreeItBase<Node>,
        Node,
        boost::forward_traversal_tag
        >
{
public:
    TreeItBase()
        : nodePtr_(0L) {}

    explicit TreeItBase(Node* node)
        : nodePtr_(node) {}

protected:
    friend class boost::iterator_core_access;

    template <class OtherNode>
    bool equal(const TreeItBase<OtherNode>& other) const
    { return nodePtr_ == other.nodePtr_; }

    virtual void increment() = 0;

    Node& dereference() const
    { return *nodePtr_; }

    Node* nodePtr_;
    std::stack<Node*> stack_;
};


template <class Node>
class PreOrderIt : public TreeItBase<Node>
{
private:
    void increment()
    { this->nodePtr_ = this->nodePtr_->right_; } // Ignore the logic error
};

我到了,

error: invalid operands to binary expression ('BinaryTree<int>::PreIt'
(aka 'PreOrderIt<BinaryTreeNode<int> >') and 'PreIt'
(aka 'PreOrderIt<BinaryTreeNode<int> >'))

这是complete error (pastebin)

修改

这是我的BinaryTree课程,

template <class T>
class BinaryTree
{
public:
    typedef TreeItBase< BinaryTreeNode<T> > It;
    typedef PreOrderIt< BinaryTreeNode<T> > PreIt;

    void Insert(const T& data);
    void Erase(It& it);

    PreIt PreOrderBegin();
    PreIt PreOrderEnd();
};

这是我的main()

int main()
{
    BinaryTree<int> tree;

    for (BinaryTree<int>::PreIt it = tree.PreOrderBegin();
        it != tree.PreOrderEnd(); ++it)
    {
        std::cout << *it << ", ";
    }
    std::cout << "\n";

    return 0;
}

virtual void increment() = 0更改为

virtual void increment()
{}

感谢大家的帮助,:)。

0 个答案:

没有答案