试图理解指针和双指针

时间:2012-07-17 21:49:33

标签: c++ pointers dereference

我正在尝试用C ++实现一个简单的链表。我可以创建节点,他们似乎正确地链接自己。我的问题涉及listIterate()函数,但我已将这些代码附加到此处以备不时之需。

#include <iostream>
using namespace std;

//Wrapper class which allows for easy list management
class LinkedList {

    //Basic node struct
    struct node {
        int data;
        node *link; 
    }; 

    node *head; //Pointer to the head (also referred to as root) node, or the first node created.
    node *current; //Pointer to the /latest/ node, or the node currently being operated on.
    node *tail; //Pointer to the tail node, or the last node in the list. 

public:

    //Default constructor. Creates an empty list.
    LinkedList() {
        head = NULL;
        current = NULL;
        tail = NULL;
        cout << "*** Linked list created. Head is NULL. ***\n";
    }

    //Default destructor. Use to remove the entire list from memory.
    ~LinkedList() {
        while(head != NULL) {
            node *n = head->link;
            delete head;
            head = n;
        }
    }

    /*
    appendNode() 
    Appends a new node to the end of the linked list. Set the end flag to true to set the last node to      null, ending the list.
    */
    void appendNode(int i) {

        //If there are no nodes in the list, create a new node and point head to this new node.
        if (head == NULL) {
            node *n = new node;
            n->data = i;
            n->link = NULL;
            head = n;

            //head node initialized, and since it is ALSO the current and tail node (at this point), we must update our pointers
            current = n;
            tail = n;
            cout << "New node with data (" << i << ") created. \n---\n"; 

        } else {

        //If there are nodes in the list, create a new node with inputted value.
        node *n = new node;
        n->data = i;
        cout << "New node with data (" << i << ") created. \n"; 

        //Now, link the previous node to this node.
        current->link = n;
        cout << "Node with value (" << current->data << ") linked to this node with     value (" << i << ").  \n---\n";     

        //Finally, set our "current" pointer to this newly created node.
        current = n;
        }
    }

    /*
    listIterate()
    Iterates through the entire list and prints every element.
    */
    void listIterate() {

        //cursor
        node *p;

        //Start by printing the head of the list.
        cout << "Head - Value: (" << head->data << ") | Linked to: (" << head->link     << ") \n";

        p = head->link;
        cout << *p;

    }

}; 

int main() {

    LinkedList List;
    List.appendNode(0);
    List.appendNode(10);
    List.appendNode(20);
    List.appendNode(30);
    List.listIterate(); 

}

现在,我将参考此方法listIterate()。

void listIterate() {

        //cursor
        node *p;

        //Start by printing the head of the list.
        cout << "Head - Value: (" << head->data << ") | Linked to: (" << head->link << ") \n";

        p = head->link; 
        cout << *p;

    }

命令cout << *p;抛出错误,我相信这就是原因:此时,p指向head->link,这是另一个指向我头节点的链接字段的指针。现在,我理解如果我在程序的这一点上取消引用p,head->link中没有实际值,因为它指向一个变量。

对我而言,如果我取消引用p两次(**p),它应该跟随指针两次(p - &gt; head->link - &gt;第二个节点的值链表(10)。但是,解除引用p两次会引发此错误。

LinkedListADT.cc:89: error: no match for ‘operator*’ in ‘** p’

任何人都可以帮助我理解为什么会这样吗?这是非法操作吗?它是以我不熟悉的另一种方式进行的吗?

3 个答案:

答案 0 :(得分:3)

cout << *p尝试打印node个对象。由于没有为节点对象定义打印操作(即输出流没有operator<<),因此打印它的尝试失败。您可能正在寻找的是:

cout << p->data;

对于你的第二点,该陈述可以这样分解:

**p == *(*p)

所以第一个明星解除引用p,返回node。第二个星试图取消引用该操作的结果,但由于节点是struct而不是指针,编译器会抱怨。

希望这有帮助。

答案 1 :(得分:1)

您的节点类缺少operator *,因此当**p类型为p时,构造node *在语义上是不正确的。要查看重载operator *的示例,请查看实现智能指针的示例。

答案 2 :(得分:1)

**p不“按指针两次”。该操作只是尝试两次取消引用p

ppointernode。第一个取消引用(*p)将评估p指向的节点。第二个取消引用(**p)会导致错误,因为nodestruct而不是pointer并且没有定义过载operator*

如果您希望取消引用指向下一个节点的指针:

*(p->link)