如何从双向链表中获取节点内的有效负载

时间:2013-02-06 17:02:27

标签: c++ class nested

template<class T> class CRevList
{
public:
//...constructor, destructor, etc;
  class Node //nested class   
  {
  public:
   friend class CRevList;

    Node() {m_next = 0;  m_prev = 0;}
    Node(const T &t) {m_payload = t;  m_next = 0;  m_prev = 0;}

    T Data() {return m_payload;}
    const T Data() const {return m_payload;}

    private:

      Node    *m_next;
      Node    *m_prev;
      T       m_payload;
  };
private: //for original class
  Node    *m_head, *m_tail;             // Head node
  unsigned size;
};

我已经多次尝试从原始的双向链接类中获取节点的有效负载,但遗憾的是我遇到了错误。最喜欢:

       error: request for member 'Data' in 'Temp1', which is of non-class type 'CRevList<int>::Node*'

我必须用两个类之间的指针或关系弄乱一些东西。

我试过了:

  //Find a node with the specified key
  const Node *Find(const T &t) const {  }
  Node *Find(const T &t) {
    Node * Temp1 = m_head;

    while(m_tail != Temp1){
            if(Temp1.Data() == t){
                    return Temp1;
            }

            Temp1 = Temp1->m_next;
    }
  }

1 个答案:

答案 0 :(得分:1)

Temp1的类型为Node *。因此,您应该拨打Temp1->Data()而不是Temp1.Data()