简体   繁体   English

'->' 标记前的预期非限定 ID

[英]expected unqualified-id before ‘->’ token

I'm trying to make a b-tree from an array, and I've came up with this code, but it's not compiling, and is giving me this error: "expected unqualified-id before '->' token" at line 42:我正在尝试从一个数组中创建一个 b 树,我想出了这个代码,但它没有编译,并且给了我这个错误:“在 '->' 标记之前预期不合格的 id”在行42:

node->balance = right_height - left_height; node->balance = right_height - left_height;

Here is the full code:这是完整的代码:

#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:请帮助D:

node is a type, not a pointer's name. node是一种类型,而不是指针的名称。 So node->balance is not syntactically correct.所以node->balance在语法上是不正确的。

node is a structure not a pointer's Name.节点是一个结构而不是指针的名称。 You want to use balance which is a variable of this structure.您想使用 balance 这是此结构的变量。 So you have to use an object of node to reach the variable balance, like root: root->balance.所以你必须使用 node 的一个对象来达到变量 balance,比如 root: root->balance。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM