【问题标题】:expected unqualified-id before ‘->’ token'->' 标记之前的预期 unqualified-id
【发布时间】:2014-09-25 15:15:08
【问题描述】:

我正在尝试从一个数组创建一个 b-tree,我想出了这段代码,但它没有编译,并且给了我这个错误: 第 42 行的“'->' 标记之前的预期 unqualified-id”:

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);
}

请帮助 D:

【问题讨论】:

  • Dumb text next because stackoverflow wasn't letting me post this, please ignore: 这是有原因的。绝对不是

标签: c++ b-tree


【解决方案1】:

node 是一种类型,而不是指针的名称。所以node-&gt;balance 在语法上是不正确的。

【讨论】:

  • 谢谢,它实际上是 root->balance :D 多么愚蠢的错误
【解决方案2】:

node 是一个结构而不是指针的名称。你想使用 balance ,它是这个结构的一个变量。所以你必须使用节点的一个对象来达到变量平衡,比如root:root->balance。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 2013-07-06
    • 1970-01-01
    相关资源
    最近更新 更多