C++ ‘之前的预期不合格id-&燃气轮机’;代币

C++ ‘之前的预期不合格id-&燃气轮机’;代币,c++,b-tree,C++,B Tree,我试图从数组中生成一个b-树,我已经得到了这段代码,但它没有编译,并且给了我以下错误: 第42行“->”标记之前的“预期非限定id”: 节点->平衡=右\u高度-左\u高度 以下是完整的代码: #include <iostream> #include <cstring> #include <cstdlib> #include <cmath> struct node { node *left; node *right; in

我试图从数组中生成一个b-树,我已经得到了这段代码,但它没有编译,并且给了我以下错误: 第42行“->”标记之前的“预期非限定id”:

节点->平衡=右\u高度-左\u高度

以下是完整的代码:

#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);
}
#包括
#包括
#包括
#包括
结构节点{
节点*左;
节点*右;
国际收支平衡;
int值;
};
node*build_子树(int*items,int-length,void*node_mem,int*height=NULL){
/*这将提供中间节点或直接到右侧的节点*/
int root_索引=长度/2;
/*创建节点。它将在其
内存块作为其值。谁需要内存管理*/
node*root=(node*)((char*)node_mem+sizeof(node)*root_index);
根->值=*(项目+根索引);
/*这些值将用于计算节点平衡*/
int left_height=0,right_height=0;
/*构建左子树*/
如果(根索引>0){
根->左=构建子树(项目,
根指数,
node_mem,
&左(U高度);
}
/*构建正确的子树*/
if(根索引<长度-1){
根->右=构建子树(项目、根索引、,
(char*)节点\u mem+sizeof(节点)*根\u索引,
&右(身高),;
}
/*计算节点的平衡和高度。
高度为1+子树高度中较大者*/
节点->平衡=右\u高度-左\u高度;
如果(高度){
*高度=(左高>右高?左高:右高)+1;
}
返回根;
}
int main(){
整数值[10000000];

对于(int i=1;i
node
是一种类型,而不是指针的名称。因此
node->balance
在语法上不正确。

node是一种结构,而不是指针的名称。您希望使用balance,它是此结构的一个变量。因此,您必须使用node的对象来达到变量平衡,如root:root->balance。

哑文本接下来,因为stackoverflow不允许我发布此消息,请忽略:
这是有原因的。这绝对不是愚蠢的!谢谢,实际上是root->balance:D多愚蠢的错误