我有這個二叉樹,其中每個結構,讓我們調用它們A有另一個結構類型的指針,讓我們調用它們B,指向另一個結構類型B等等形成一個結構類型B)的鏈表。從不兼容的指針類型/ deref指向不完整類型的指針
圖片:
(A)
/\
(A)->(B)->(B)->(B)->||
的問題,我不知道。我收到一個錯誤,指出:
AddRemove.c: In function ‘AddRemove’:
AddRemove.c:21: warning: assignment from incompatible pointer type
AddRemove.c:22: error: dereferencing pointer to incomplete type
AddRemove.c:23: error: dereferencing pointer to incomplete type
AddRemove.c:24: error: dereferencing pointer to incomplete type
AddRemove.c:26: error: dereferencing pointer to incomplete type
代碼:
struct A{
//other variables
struct A *left,*right;
struct B *queue;
}*rootT;
struct B{
//other variables
struct B *next;
};
void AddRemove(struct A *aNode, struct B *bNode){
/*aNode is the memory location of the struct node (A) in the picture and bNode is
a struct node that we want to add to the linkedlist.*/
struct B *Bptr; //Used to go through the linkedlist of struct type B
if(aNode->queue==NULL){ /*If the pointer of node (A) is null then we have the
pointer point to bNode, the node that we wanted to add.*/
aNode->queue=bNode;
bNode->next=NULL;
}
else{
Bptr=aNode->queue; /*Otherwise have the temp pointer point to what
node (A)'s pointer points to, which should be the first struct type (B)*/
while(Bptr->next!=NULL){ /*Keep pointing to the next struct of type B until
we hit the end*/
Bptr=Bptr->next;
}
Bptr->next=bNode;
}
}
在定義struct A之前添加聲明:'struct B;' –
這並沒有改變我收到的錯誤。我應該提到這些結構聲明是在一個單獨的.h文件中。 – user2644819
你是否在這個源代碼中包含頭文件?如果沒有,你需要這樣做。如果您不將它們包含在翻譯單元中,編譯器將無法對這些類型進行任何處理。 –