2016-06-13 179 views
1

我試圖採用RRhead,在RRhead上有一個新的RCB點,因爲它是下一個,並且在新的RCB處有RRhead點,因爲它是先前的點。C結構體的不完整類型

typedef struct{ 
    int sequence_number; 
    int file_descriptor; 
    FILE *requested_file; 
    int bytes_remaining; 
    int quantum; 
    struct RCB *next; 
    struct RCB *prior; 
} RCB; 

typedef struct RCB RCB; 


RCB *RRhead = NULL; 


static void admit_to_scheduler_RR(int fd, FILE *fin){ 
int sequence_counter, new_bytes_remaining, new_quantum = 0; 
    RCB new_rcb = {sequence_counter, fd, fin, new_bytes_remaining, new_quantum, RRhead, NULL}; 
    RRhead->prior = &new_rcb; 
    RRhead = &new_rcb; 
    sequence_counter++; 
} 

產生以下錯誤:

sws.c: In function ‘admit_to_scheduler_RR’: 
sws.c:318:10: error: variable ‘new_rcb’ has initializer but incomplete type 
    struct RCB new_rcb = {sequence_counter, fd, fin, new_bytes_remaining, new_quantum, RRhead, NULL}; 
     ^
sws.c:318:10: warning: excess elements in struct initializer [enabled by default] 
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default] 
sws.c:318:10: warning: excess elements in struct initializer [enabled by default] 
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default] 
sws.c:318:10: warning: excess elements in struct initializer [enabled by default] 
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default] 
sws.c:318:10: warning: excess elements in struct initializer [enabled by default] 
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default] 
sws.c:318:10: warning: excess elements in struct initializer [enabled by default] 
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default] 
sws.c:318:10: warning: excess elements in struct initializer [enabled by default] 
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default] 
sws.c:318:10: warning: excess elements in struct initializer [enabled by default] 
sws.c:318:10: warning: (near initialization for ‘new_rcb’) [enabled by default] 
sws.c:318:14: error: storage size of ‘new_rcb’ isn’t known 
    struct RCB new_rcb = {sequence_counter, fd, fin, new_bytes_remaining, new_quantum, RRhead, NULL}; 
      ^
sws.c:319:9: error: dereferencing pointer to incomplete type 
    RRhead->prior = &new_rcb; 
     ^

我不知道爲什麼我收到了多餘的元素警告線索。我沒有正確初始化new_rcb嗎?我是否需要創建它,然後將所有字段設置爲我想要的?

我相信這一切都與new_rcb有一個「不完整的類型」有關。大多數使用Google表示這是因爲編譯器不知道RCB的大小應該是多少。它似乎表明我需要將此結構定義放入一個標題中。這是絕對必要的嗎?

+0

'typedef struct RCB RCB;'嘗試刪除該文件 –

+0

刪除該文件會導致此函數發生不同的錯誤。 –

回答

2

在第一個聲明中,您聲明RCBstruct使用typedef。直接後來,你說,'順便說一句,RCB意味着struct RCB',這是沒有定義在這一點! (對於存在的,你不得不說struct RCB { ... };代替。)


基本上,刪除typedef struct RCB RCB;這將擺脫對相關錯誤的,但新會彈出。

1

嘗試你的結構更改爲以下(即刪除typedef關鍵字):

struct RCB { 
    int sequence_number; 
    int file_descriptor; 
    FILE *requested_file; 
    int bytes_remaining; 
    int quantum; 
    struct RCB *next; 
    struct RCB *prior; 
}; 

的更多信息可以在這個答案讀給Difference between 'struct' and 'typedef struct' in C++?

1

嘗試這樣:

typedef struct RCB RCB; 
strutc RCB { 
.... 

「標籤」命名空間中的C(與structunion推出)是從標識符名字空間不同。使用typedef,您還可以將標識符RCB與類型struct RCB關聯。

0
#include <stdio.h> 
#include <stdlib.h> 

typedef struct RCB RCB; 

typedef struct RCB { 
    int sequence_number; 
    int file_descriptor; 
    FILE *requested_file; 
    int bytes_remaining; 
    int quantum; 
    RCB *next; 
    RCB *prior; 
} RCB; 

RCB new_rcb = {11, 21, NULL, 31, 41, NULL, NULL};; 

static void admit_to_scheduler_RR(int fd, FILE *fin) { 

    int sequence_counter = 0; 
    int new_bytes_remaining, new_quantum = 0; 
    RCB *RRhead = &new_rcb; 
    RRhead->prior = &new_rcb; 
    sequence_counter++; 
} 

int main(int argc, char *argv[]) { 

    admit_to_scheduler_RR(1, NULL); 
    return 0; 
} 

要修復你的錯誤,你需要更改您的代碼類似於上面的東西,它使用你的數據結構和編譯。

相關問題