c++ - 试图理解指针和双指针

标签 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 -> head->link -> 链表中第二个节点的值(10)。但是,取消引用 p 两次会引发此错误。

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

谁能帮我理解为什么会这样?这是非法操作吗?是否以我不熟悉的其他方式执行?

最佳答案

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

cout << p->data;

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

**p == *(*p)

所以第一个星取消引用 p , 返回 node .第二颗星尝试取消引用该操作的结果,但由于节点是 struct而不是指针,编译器会提示。

希望这对您有所帮助。

关于c++ - 试图理解指针和双指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11531371/

相关文章:

c++ - 在 C++ 计算器中使用模板

c++ - "static"关键字与变量、函数和类对象等有什么用?

c - 二进制搜索树插入不起作用

C++ "vector iterator not dereferencable"在运行时

c - **在C语言中是什么意思?

c++ - 关于纯抽象类中的静态成员函数 - 设计模式?

c++ - const union成员有什么用?他们不是毫无意义吗?

pointers - 如何在不指定类型的情况下修改函数输入参数?

pointers - Go:将数组的指针传递给 gob 而不进行复制?

pointers - 使用 xlsx 包 panic : runtime error: invalid memory address or nil pointer dereference Go