"->"令牌之前的预期非限定 ID
expected unqualified-id before ‘->’ token
我正试图从数组中创建一个b树,我已经想出了这个代码,但它没有编译,并且给了我这个错误:" ' -> '令牌前预期的非限定id ",在第42行:
node->balance = right_height - left_height;
下面是完整的代码:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
struct node {
node *left;
node *right;
int balance;
int value;
};
node *build_subtree(int *items, int length, void* node_mem, int *height = NULL) {
/*This will give either the middle node or immediately to the right.*/
int root_index = length / 2;
/* Make the node. It will be at the same index in its
memory block as its value. Who needs memory management? */
node *root = (node*)((char*)node_mem + sizeof(node) * root_index);
root->value = *(items + root_index);
/* These values will be used to compute the node balance */
int left_height = 0, right_height = 0;
/* Build the left subtree */
if (root_index > 0) {
root->left = build_subtree(items,
root_index,
node_mem,
&left_height);
}
/* Build the right subtree */
if (root_index < length - 1) {
root->right = build_subtree(items, root_index,
(char*)node_mem + sizeof(node) * root_index,
&right_height);
}
/* Compute the balance and height of the node.
The height is 1 + the greater of the subtrees' heights. */
node->balance = right_height - left_height;
if (height) {
*height = (left_height > right_height ? left_height : right_height) + 1;
}
return root;
}
int main() {
int values[10000000];
for (int i=1; i<=10000000; i++)
values[i] = i;
void *mem = malloc(sizeof(node) * 10000000);
memset(mem, 0, sizeof(node) * 10000000);
node *root = build_subtree(values, 10000000, mem);
}
Please help D:
node
是一个类型,而不是指针的名称。所以node->balance
在语法上是不正确的
node是一个结构体,不是指针的名称。你想用平衡,它是这个结构的一个变量。因此,您必须使用节点对象来达到变量平衡,如root: root->balance。
相关文章:
- 从函数角度看ID到文件路径的内部与外部映射
- 通过组合不同的类型来创建唯一的id
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- EASTL矢量<向量<int>>连续的
- FFMPEG配置文件级别id大小无效
- 方法内部但循环仍得到预期的不合格id错误C++
- 如何获取 GLFW 窗口 ID?
- 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断
- 显示数组中的学生 ID 和最高分
- C++:"("令牌"之前有预期的非限定 id 指向类中成员函数的指针
- 在PostgreSQL中根据它们的ID选择大量行的最快方法是什么?
- Xcode 9.4.1 中的 Apple Mach-O 链接器 (id) 错误
- 错误:令牌 { '{' 之前应存在非限定 ID
- 断言"id < 0"在Qt ActiveX中失败
- C++部分概念 id:显式模板规范顺序/第一个参数的特殊状态的原因是什么?
- 如何根据两个因素组织向量:id 和数量?(C++)
- 在返回 0 之前应为非限定 ID
- Direct3D 11 - HLSL - 获取顶点索引 ID
- 编译时检查特征专用化是否具有唯一 ID
- DCMTK 了解"DIMSE 没有有效的演示上下文 ID"错误