C++双链表迭代器

C++双链表迭代器,c++,C++,什么 我在这里失踪了吗?以下是我在代码中遇到的错误: 错误: Iterator.cpp:4:11: error: 'Iterator' in 'class NodeList' does not name a type NodeList::Iterator::Iterator(Node* u) // constructor from Node* ^ Iterator.cpp:7:19: error: 'NodeList::Iterator' has not been d

什么 我在这里失踪了吗?以下是我在代码中遇到的错误:

错误:

Iterator.cpp:4:11: error: 'Iterator' in 'class NodeList' does not name a type
 NodeList::Iterator::Iterator(Node* u)  // constructor from Node*
           ^

Iterator.cpp:7:19: error: 'NodeList::Iterator' has not been declared
   Elem& NodeList::Iterator::operator*()  // reference to the element
                   ^

Iterator.cpp:7:39: error: 'Elem& operator*()' must have an argument of class or enumerated type
   Elem& NodeList::Iterator::operator*()  // reference to the element
                                       ^

Iterator.cpp:10:18: error: 'NodeList::Iterator' has not been declared
   bool NodeList::Iterator::operator==(const Iterator& p) const
                  ^

Iterator.cpp:10:58: error: non-member function 'bool operator==(const Iterator&)' cannot have cv-qualifier
   bool NodeList::Iterator::operator==(const Iterator& p) const
                                                          ^

Iterator.cpp:10:58: error: 'bool operator==(const Iterator&)' must take exactly two arguments
Iterator.cpp:13:18: error: 'NodeList::Iterator' has not been declared
   bool NodeList::Iterator::operator!=(const Iterator& p) const
                  ^

Iterator.cpp:13:58: error: non-member function 'bool operator!=(const Iterator&)' cannot have cv-qualifier
   bool NodeList::Iterator::operator!=(const Iterator& p) const
                                                          ^

Iterator.cpp:13:58: error: 'bool operator!=(const Iterator&)' must take exactly two arguments
Iterator.cpp:16:13: error: 'Iterator' in 'class NodeList' does not name a type
   NodeList::Iterator& NodeList::Iterator::operator++()
             ^

Iterator.cpp:19:13: error: 'Iterator' in 'class NodeList' does not name a type
   NodeList::Iterator& NodeList::Iterator::operator--()
             ^
守则:

#ifndef NODE_H
#define NODE_H
#include <string>
using namespace std;
typedef string Elem;
struct Node 
{               // a node of the list
    Elem elem;              // element value
    Node* prev;             // previous in list
    Node* next;             // next in list
};


#endif  /* NODE_H */

#ifndef ITERATOR_H
#define ITERATOR_H
#include "Node.h"
class Iterator
{               // an iterator for the list
   private:
     Node* v;                   // pointer to the node
     Iterator(Node* u);         // create from node
   public:
     Elem& operator*();         // reference to the element
     bool operator==(const Iterator& p) const;  // compare positions
     bool operator!=(const Iterator& p) const;
     Iterator& operator++();            // move to next position
     Iterator& operator--();            // move to previous     
     friend class NodeList;         // give NodeList access

 };


 #endif /* ITERATOR_H */


#ifndef NODELIST_H
#define NODELIST_H
#include "Node.h"
#include "Iterator.h"
typedef int Elem;               // list base element type
class NodeList
{               // node-based list
  public:
    NodeList();                 // default constructor
    int size() const;               // list size
    bool empty() const;             // is the list empty?
    Iterator begin() const;         // beginning position
    Iterator end() const;           // (just beyond) last position
    void insertFront(const Elem& e);        // insert at front
    void insertBack(const Elem& e);     // insert at rear
    void insert(const Iterator& p, const Elem& e); // insert e before p
    void eraseFront();              // remove first
    void eraseBack();               // remove last
    void erase(const Iterator& p);      // remove p
    // housekeeping functions omitted...
  private:                  // data members
    int     n;                  // number of items
    Node*   header;             // head-of-list sentinel
    Node*   trailer;                // tail-of-list sentinel
};


#endif  /* NODELIST_H */

#include "NodeList.h"
#include <iostream>
using namespace std;
NodeList::NodeList()
{           // constructor
   n = 0;                   // initially empty
   header = new Node;               // create sentinels
   trailer = new Node;
   header->next = trailer;          // have them point to each other
   trailer->prev = header;
}

int NodeList::size() const          // list size
  { return n; }

bool NodeList::empty() const            // is the list empty?
  { return (n == 0); }

NodeList::Iterator NodeList::begin() const  // begin position is first item
  { return Iterator(header->next); }

NodeList::Iterator NodeList::end() const    // end position is just beyond last
  { return Iterator(trailer); }
void NodeList::erase(const Iterator& p) 
{   // remove p
  Node* v = p.v;                // node to remove
  Node* w = v->next;                // successor
  Node* u = v->prev;                // predecessor
  u->next = w;  w->prev = u;            // unlink p
  delete v;                 // delete this node
  n--;                  // one fewer element
}

void NodeList::eraseFront()         // remove first
{ erase(begin()); }

void NodeList::eraseBack()          // remove last
{ erase(--end()); }
void NodeList::insert(const NodeList::Iterator& p, const Elem& e)
{
   Node* w = p.v;               // p's node
   Node* u = w->prev;               // p's predecessor
   Node* v = new Node;              // new node to insert
   v->elem = e;
   v->next = w;  w->prev = v;           // link in v before w
   v->prev = u;  u->next = v;           // link in v after u
   n++;
}

void NodeList::insertFront(const Elem& e)   // insert at front
{ insert(begin(), e); }

void NodeList::insertBack(const Elem& e)    // insert at rear
{ insert(end(), e); }

#include "Iterator.h"
#include <iostream>
using namespace std;
NodeList::Iterator::Iterator(Node* u)       // constructor from Node*
  { v = u; }

Elem& NodeList::Iterator::operator*()       // reference to the element
  { return v->elem; }
                    // compare positions
bool NodeList::Iterator::operator==(const Iterator& p) const
  { return v == p.v; }

bool NodeList::Iterator::operator!=(const Iterator& p) const
  { return v != p.v; }
                    // move to next position
NodeList::Iterator& NodeList::Iterator::operator++()
  { v = v->next; return *this; }
                    // move to previous position
NodeList::Iterator& NodeList::Iterator::operator--()
  { v = v->prev; return *this; }

您的迭代器类是在全局命名空间中定义的,但是当您定义其方法时,您将其限定为NodeList的嵌套类:NodeList::Iterator应该只是迭代器。

请提取一个,这里有太多未使用的代码。建议:一次编写更少的代码。做一个空的类。编译、测试。添加您认为需要的成员。编译、测试。添加一个函数。编译、测试。添加一个函数。编译、测试。重复,直到完成。踢球者被提前和经常测试。在错误积累之前抓住它们,你必须把几十个错误整理出来。