试图理解指针和双指针
Trying to understand pointers and double pointers
我正在尝试在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:错误:与"** p"中的"运算符*"不匹配
谁能帮我理解为什么会这样?这是非法操作吗?它是否以我不熟悉的另一种方式执行?
>cout << *p
尝试打印node
对象。由于没有为节点对象定义打印操作(即输出流没有operator<<
(,因此尝试打印操作失败。您可能正在寻找的是:
cout << p->data;
对于您的第二点,该语句可以分解为:
**p == *(*p)
所以第一颗星p
去引用,返回一个node
。第二个星号试图取消引用该操作的结果,但由于节点是struct
而不是指针,因此编译器会抱怨。
希望这有帮助。
您的节点类缺少operator *
,因此当p
类型为 node *
时,构造**p
在语义上格式不正确。要查看重载operator *
的示例,请查看实现智能指针的示例。
**p
不"跟随指针两次"。该操作只是尝试取消引用p
两次。
p
是node
的pointer
。第一个取消引用 (*p
( 将计算到 p
指向的节点。第二次取消引用(**p
(将导致错误,因为node
是struct
而不是pointer
,并且没有定义重载operator*
。
如果要取消引用指向下一个节点的指针:
*(p->link)
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- QMetaObject invokeMethod的基于函数指针的语法
- 如何从 std::atomic 中提取指针 T<T>?
- 如何在 C# 中映射双 C 结构指针?
- C++将浮点指针值舍入为小数位数
- 为什么++(*p)更改指针值
- 调整大小后指向元素值的指针unordered_map有效?
- 正在将指针转换为范围
- 使用指向成员的指针将成员函数作为参数传递
- 将OpenCV C++重写为EmguCV C#-如何使用指针
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错