2016-06-13 30 views
1

バイナリ検索ツリーのイテレータを実装しようとしています。私はイテレータでSTLを使用しないように頼まれました。私は演算子を無効にする必要があります:++operator*、および!=operator*でエラーが表示されます。「*operatorはこのオペランドと一致しません。オペランドタイプは*iterator<std::string>」です。テンプレートライブラリを使用しているので、なぜ動作しないのか分かりません。バイナリ検索ツリーinorderイテレータC++

template <typename T> 
class Iterator : public std::iterator<std::forward_iterator_tag, { 

public: 
    Iterator(TreeNode<T>* root) 
    { 
     this->current = root; 
    } 

    template <typename T> 
    bool operator!=(Iterator<T> const & other) const 
    { 
     return this->current != other.current; 
    } 

    template <typename T> 
    T &operator*() const { 
     return current->element; 
    } 


    Iterator operator++() 
    { 
     current = current->nextInorder(); 
     return *this; 
    } 

    Iterator operator++(int dummy) 
    { 
     TreeNode<T> temp = current; 
     current = current->nextInorder(); 
     return *temp; 
    } 

private: 
    TreeNode<T>* current; 
    void nextInorder() 
    { 
     if (current->element == NULL)return; 
     else { 
      nextInorder(current->left); 
      nextInorder(current->element); 
      nextInorder(current->right); 
     } 
    } 

}; 

答えて

0

コードがうまく貼り付けられません(class Iterator...ラインを参照してください):

は、ここに私のコードです。 template <typename T>bool operator!=(Iterator<T> const & other) constT &operator*() constの方法で削除することをおすすめします。 Tはクラスのインスタンス化に使用されるものです。

関連する問題