我實現在C.隊列的實現我的接口包括5個簡單的函數來訪問隊列在線聲明:混淆使用C
#ifndef QUEUE_H
#define QUEUE_H
#include <stdbool.h>
#include <stddef.h>
struct queue {
struct cell* first;
struct cell* last;
};
typedef struct queue queue;
extern queue newQueue(void);
extern bool isEmpty(queue);
extern queue enqueue(queue,void*);
extern queue dequeue(queue);
extern void* front(queue);
extern void freeQueue(queue);
因爲他們兩個(newQueue
和isEmpty
)是如此平凡我相信編譯器可以對它們做很多優化,我決定爲它們編寫內聯聲明:
/* replacing the two lines
extern queue newQueue(void);
extern bool isEmpty(queue);
in the original header */
extern inline queue newQueue(void) {
queue q = { NULL, NULL };
return q;
}
extern inline bool isEmpty(queue q) {
return q.first == NULL;
}
這個編譯和gcc很好。但是當我用clang編譯它時,它給了我一個錯誤。一個快速的研究表明,從GNU風格做這些內聯聲明is different的官方方式。我可以通過-std=gnu89
或根據上面的鏈接更改功能簽名。我chosed第二個選項:
inline queue newQueue(void) {
queue q = { NULL, NULL };
return q;
}
inline bool isEmpty(queue q) {
return q.first == NULL;
}
但現在,無論鐺和gcc說一些關於重複函數聲明,在C99編譯模式。這是queue.c
中的相關定義:
#include "queue.h"
/* ... */
queue newQueue() {
queue q = { NULL, NULL };
return q;
}
bool isEmpty(queue q) {
return q.first == NULL;
}
我在做什麼錯了?我怎樣才能得到我想要的,而無需切換到gnu89模式?
這些都是錯誤的消息我與第二風格得到:
$ gcc -std=c99 queue.c
queue.c:12:7: error: redefinition of ‘newQueue’
queue.h:14:21: note: previous definition of ‘newQueue’ was here
queue.c:17:6: error: redefinition of ‘isEmpty’
queue.h:19:20: note: previous definition of ‘isEmpty’ was here
$ clang -std=c99 queue.c
queue.c:12:7: error: redefinition of 'newQueue'
queue newQueue() {
^
In file included from queue.c:5:
./queue.h:14:21: note: previous definition is here
extern inline queue newQueue(void) {
^
queue.c:17:6: error: redefinition of 'isEmpty'
bool isEmpty(queue q) {
^
In file included from queue.c:5:
./queue.h:19:20: note: previous definition is here
extern inline bool isEmpty(queue q) {
^
2 errors generated.
你能證明你得到確切* *錯誤信息? 「關於重複函數定義的一些事情」還不夠具體。 –
@Greg對不起,忘記了錯誤信息。我已經添加了它。 – fuz