Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
463 views
in Technique[技术] by (71.8m points)

c++ - 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:

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:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

node is a type, not a pointer's name. So node->balance is not syntactically correct.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...