错误:'root'没有为类型

时间:2015-12-30 20:22:05

标签: c++ struct types g++ incomplete-type

对于节点的所有属性以及所有节点,我收到以下错误。

n-tree.h:35:2: error: ‘root’ does not name a type
  root->parent = NULL;
  ^

我想创建一个具有任意数量子节点的树。我为树定义创建了一个头文件。

我首先将节点定义为结构。 然后使用Node的定义,我创建了一个Tree。

我尝试了很多方法来解决错误,例如,通过在nTree结构中定义Node结构,在其他常见实现中定义节点。

这是n-tree.h

#ifndef N_TREE
#define N_TREE
#include <cstdio>

struct Node
{
    Node* parent;
    Node* left_child;
    Node* right_sibling;
    double key;
};

struct nTree
{

// Nodes - attributes of the tree
Node* root;
Node* node1_1;
    Node* node1_1_1;
    Node* node1_1_2;

Node* node1_2;
    Node* node1_2_1;
    Node* node1_2_2;
    Node* node1_2_3;

Node* node1_3;
    Node* node1_3_1;



// Root level
root->parent = NULL;
root->left_child = node1_1;
root->right_sibling = NULL;
root->key = 1;

// Level 1_1
node1_1->parent = root;
node1_1->left_child = node1_1_1;
node1_1->right_sibling = node1_2;
node1_1->key = 2;

// Level 1_2
node1_2->parent = root;
node1_2->left_child = node1_2_1;
node1_2->right_sibling = node1_3;
node1_2->key = 3;

// Level 1_3
node1_3->parent = root;
node1_3->left_child = node1_3_1;
node1_3->right_sibling = NULL;
node1_3->key = 4;


// Level 1_1_1
node1_1_1->parent = node1_1;
node1_1_1->left_child = NULL;
node1_1_1->right_sibling = node1_1_2;
node1_1_1->key = 5;

 // Level 1_1_2
node1_1_2->parent = node1_1;
node1_1_2->left_child = NULL;
node1_1_2->right_sibling = NULL;
node1_1_2->key = 6;

// Level 1_2_1
node1_2_1->parent = node1_2;
node1_2_1->left_child = NULL;
node1_2_1->right_sibling = node1_2_2;
node1_2_1->key = 7;

// Level 1_2_2
node1_2_2->parent = node1_2;
node1_2_2->left_child = NULL;
node1_2_2->right_sibling = node1_2_3;
node1_2_2->key = 8;

// Level 1_2_3
node1_2_3->parent = node1_2;
node1_2_3->left_child = NULL;
node1_2_3->right_sibling = NULL;
node1_2_3->key = 9;

// Level 1_3_1
node1_3_1->parent = node1_3;
node1_3_1->left_child = NULL;
node1_3_1->right_sibling = NULL;
node1_3_1->key = 10;

};
#endif

这是n-tree_implement.cpp

#include <cstdio>
#include "n-tree.h"

int main()
{
    nTree* tree = new nTree;
    printf("%d\n", tree->root->key);
    delete tree;
}

上面包含的头文件为nTree中的所有节点提供了以下错误。

1 个答案:

答案 0 :(得分:4)

行:

root->parent = NULL;
root->left_child = node1_1;
root->right_sibling = NULL;
root->key = 1;

不合法。

我对建议解决方案犹豫不决,因为从您的代码中不清楚您打算如何使用nTree