2014-09-19 151 views
0

所以我试图构造一个B树,使用两种类型的节点,单个关键节点(kNode)和包含基于顺序大小(sibNode)的k个节点数量的超级节点。我一直遇到的问题是,为了这个设置工作,我需要sibNode中的kNode指针(例如,pkey和smallestPnt)以及kNode中的sibNode指针(例如,nxtkey和child)。不管我首先放哪个typedef,都会返回这些指针的错误未知类型(例如按照以下顺序返回:error:unknown type name'kNode')。如果有人可以给我一些关于如何避免这个错误的建议,将不胜感激。C typedef冲突

typedef int keyT; 

//B-Tree Node typdef 
typedef struct 
{ 
int size; 
int cursor; 
kNode* pkey; 
kNode* smallestPnt; 

}sibNode; 

//key Node typedef 
typedef struct 
{ 

keyT* key; 
sibNode* nxtkey; 
sibNode* child; 

}kNode; 

回答

4

sibNode类型被定义,类型kNode尚未定义。

使用前向声明是这样的:

struct kNode;  //this is forward declaration 

typedef struct 
{ 
    int size; 
    int cursor; 
    struct kNode* pkey;   //here 
    struct kNode* smallestPnt; //here 
}sibNode; 

typedef struct kNode  //and here 
{ 
    keyT* key; 
    sibNode* nxtkey; 
    sibNode* child; 
}kNode; 
+2

+1,并且值得注意的是,东方电气'结构kNode独立前行;'是可选的。将两个成员更改为'struct kNode *'并正确地标记预期的结构将做到这一点。 – WhozCraig 2014-09-19 05:04:32