簡體   English   中英

錯誤:取消引用不完整類型的指針

[英]error: dereferencing pointer to incomplete type

瀏覽了許多與此相關的其他SO帖子,但沒有一個能夠幫助我。 因此,我定義了以下結構:

 typedef struct
    {
    int created;
    double data;
    int timeLeft;
    int destination;
}dataPacket;

typedef struct
{
    dataPacket *array;
    int currIndex;
    int firstIndex;
    int nextTick;
    int maxLength;
    int length;
    int stime;
    int total;


}packetBuffer;

typedef struct{
    int mac;
    struct wire *lconnection;
    struct wire *rconnection;
    int numRecieved;
    struct packetBuffer *buffer;
    int i;
    int backoff;
}node;

typedef struct{
    float length;
    float speed;
    int busy;
    struct dataPacket *currPacket;
    struct node *lnode;
    struct node *rnode;
}wire;

然后,我嘗試使用以下功能:

    int sendPacket(node *n, int tick)
{
    if(n->buffer->length > 0)
    {
        if(n->backoff <= 0)
        {
            if (n->lconnection->busy != 0 || n->lconnection->busy != 0)
            {
                n->i++;
                n->backoff = (512/W * genrand()*(pow(2,n->i)-1))/TICK_LENGTH;
            }
            else
            {
                n->lconnection->busy = 1;
                n->rconnection->busy = 1;
                n->lconnection->currPacket = n->buffer[n->buffer->currIndex];
                n->rconnection->currPacket = n->buffer[n->buffer->currIndex];
            }
        }
        else
        {
            n->backoff--;
        }
    }
}

每次嘗試訪問buffer,lconnection或rconnection的成員時,都會出現標題中描述的錯誤。

struct packetBuffer *buffer;

您已經定義了一個packetBuffer類型(用於其他匿名結構的typedef)。

您尚未定義struct packetBuffer

在沒有現有類型struct packetBuffer的情況下,編譯器將其視為不完整類型,假設您稍后將完成它。 報關單

struct packetBuffer *buffer;

是完全合法的,但是除非可見struct packetBuffer類型,否則您struct packetBuffer引用buffer

只需刪除struct關鍵字。

(我個人的喜好是刪除typedef並將結構類型始終稱為struct whatever ,但這是樣式和風格的問題。)

下列:

typedef struct { int x; char *y; ... } my_struct;

創建一個匿名結構的標識符。 為了使結構引用其自身的實例,它不得為“匿名”:

typedef struct my_struct {
    int x;
    char *y;
    struct my_struct *link
    ....
} my_struct_t;

這意味着my_struct_t現在是struct my_struct類型,而不僅僅是匿名struct。 另外,請注意struct my_struct可以在其自己的結構定義中使用。 使用匿名結構是不可能的。

作為最后的復雜化, my_structstruct my_struct處於differenct“命名空間”比my_struct_t 有時這是用來簡化(或混淆)如下代碼中的內容:

typedef struct my_struct {
    int x;
    char *y;
    struct my_struct *link
    ....
} my_struct;

現在,我可以在代碼的任何地方使用my_struct ,而不是struct my_struct

最后,您可以將typedef與結構定義分開,以達到相同的效果:

struct my_struct {
    int x;
    char *y;
    struct my_struct *link;
    ....
};
typedef struct my_struct my_struct;

如David R.Hanson的C接口和實現所述 ,“此定義是合法的,因為結構,聯合和枚舉標記占用的名稱空間與變量,函數和類型名稱的空間分開。”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM