C 中关于节点的错误
error about a node in C++
我有这个错误,我不知道如何修复它。
error: must use 'struct' tag to refer to type 'node' in this scope
node *node = new node;
我的代码在哪里。
//New empty tree
struct node *newTreeNode(int data)
{
//New tree nodes
node *node = new node;
//New data node
node->data = data;
//New left node
node->left = nullptr;
//New right node
node->right = nullptr;
return node;
}
^
此错误来自于声明具有与其类型相同名称的对象的陌生性:
node *node = new node;
这不仅对您程序的读者感到非常困惑,而且现在在RHS上,node
一词表示对象,而不是类型。因此new node
变得无效。
错误消息会通知您,您可以通过在 type 上写下struct
来进行node
:
node* node = new struct node;
这有效,因为当T
是类类型时,struct T
总是表示类型T
,并且不能含义其他任何内容。
但是,老实说,根本不这样做。使用更好的名称。
您已声明了一个称为node
的变量。那是您打算在该声明之后使用的类型的名称。因此,您需要指定您指的是类型,而不是通过适当使用struct
或class
的变量。
node *node = new struct node;
^^^^^^
更好的解决方案是为变量使用其他名称。
node* n = new node;
相关文章:
- 删除链表中的节点 - 分段错误
- C++错误(从不兼容的类型"void"分配给树节点*)
- 节点 API 抛出自己的错误消息,而不是自己的错误消息
- 树节点之间的最大距离中的运行时错误
- V8 - 节点C++插件 - 在构造函数中引发异常会导致"致命错误:v8::ToLocalChecked Em
- 在BST堆中删除节点免错误
- 解析序列/映射节点时出现无效的 yaml 节点错误
- 节点C++源代码编译错误
- 错误:无法从节点*转换为节点 c++?
- 节点插件编译错误
- 当我将字符串放入节点>名称时,为什么会出现分段错误(核心转储)?
- 分段错误,同时减去链表的 2 个连续节点
- 在 BST 中插入节点时出现逻辑错误
- 当我返回节点*时,C++中出现未知类型错误
- 插件中节点.js/Nan 回调C++不频繁的隔离错误
- YAML-CPP 节点诊断有意义的错误
- 使用括号会在声明新节点时会产生错误
- C++为什么创建一个新节点来删除堆栈上的节点是错误的
- 在链表运行时插入节点错误
- 无法在 Borland C++ 中将'void*'转换为 *poly 节点错误