为链表C++创建节点
Create Node for linkedlist C++
我正在使用c++实现链表。我在LinkedList.h中创建了一个结构Node,并试图重载节点中的运算符。但当我编译时,我得到了这个错误代码:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
class LinkedList{
typedef struct Node{
int data;
Node* next;
} * nodePtr;
//Returns true if the current Node object value is
//less than the parameter Node object value
bool operator < (const Node& node) const {
return this->data < node->data; <--- Unable to resolve identifier data.
};
#endif /* LINKEDLIST_H */
我不知道我做错了什么。有人能告诉我吗?!谢谢
尽管我会用不同的方法来做这件事,但问题是您没有在类中定义任何位置来容纳Node结构。我不确定你是否在尝试:
class LinkedList{
typedef struct Node{
int data;
Node* next;
} * nodePtr;
Node node; // Added this
//Returns true if the current Node object value is
//less than the parameter Node object value
bool operator < (const Node& node) const {
return this->node.data < node.data;
}
};
看起来您正在尝试访问不存在的东西。您的LinkedList
实现没有名为data
的变量。
最简单的修复方法是更改operator
主体:
return this->nodePtr->data < node->data;
然而,我建议重构为Node
提供一个完全独立的类;您可以将运算符重载放在该类中。
您将节点作为引用传递,因此应该使用node.data
还删除关键字typedef
,因为它使您只定义类型,并且您的列表确实需要指向第一个节点的指针!
然后您必须将您的退货更新为:
return this->nodePtr->data < node.data;
相关文章:
- 无法为 X.radio 创建中等可视化,因为找不到网络节点可视化 - Omnet++
- 创建了一个链表,但如何删除 c++ 中的"所有"节点
- 在解决链表问题时创建一个额外的节点是一个好习惯吗?
- 如何在节点插件中创建 getter?
- C++创建指向有向图中节点的指针数组
- 如果我们不创建一个新节点并使用指针插入数据并建立链接(在链表中)怎么办?
- 创建附加到每个节点的节点列表 (c++)
- 如何在c++中创建节点
- 为什么我的类节点覆盖自身而不是创建新的节点对象
- 创建的节点类型不同
- 在不创建新节点的情况下实现带有映射的trie
- 创建静态哨兵节点的正确方法是什么
- C++创建一个链表,每个节点有超过 2 个字段
- 插入功能保持重新创建根节点
- 我们不能在不使用指向对象的指针的情况下创建节点C++吗?
- 为数据缺少默认构造函数的链表创建节点
- 在声明节点创建链接列表时,为什么静态内存分配不起作用
- 如何用空节点创建完整的二进制树
- 使用可移动节点创建有向图(使用 QT / Boost)
- C++链表节点创建导致无限循环