我想知道如何解决重新定义typedef结构QueueADT的错误。
错误消息
gcc -std=c99 -ggdb -Wall -Wextra -c queueADT.c
queueADT.c:22:3: error: 'QueueADT' redeclared as different kind of symbol
}*QueueADT;
^
In file included from queueADT.c:9:0:
queueADT.h:23:21: note: previous declaration of 'QueueADT' was here
typedef struct { } *QueueADT;
^
queueADT.c:30:1: error: unknown type name 'QueueADT'
QueueADT que_create( int (*cmp)(const void*a,const void*b) ) {
^
queueADT.c:30:10: error: conflicting types for 'que_create'
QueueADT que_create( int (*cmp)(const void*a,const void*b) ) {
^
In file included from queueADT.c:9:0:
queueADT.h:44:10: note: previous declaration of 'que_create' was here
QueueADT que_create( int (*cmp)(const void*a,const void*b) );
^
如果没有编译,则.h文件有一个定义,即queueADT和que_create
#ifndef _QUEUE_IMPL_
typedef struct { } *QueueADT;
#endif
QueueADT que_create( int (*cmp)(const void*a,const void*b) );
现在我不知道如何在这里从.c文件定义QueueADT结构和que_create
#ifndef _QUEUE_IMPL_
#include "queueADT.h"
struct node {
void* data;
//size_t num;
struct node *next;
}node;
typedef struct QueueADT {
struct node *front; /* pointer to front of queue */
struct node *rear; /* pointer to rear of queue */
int *contents[ QUEUE_SIZE ]; /* number of items in queue */
int *cmprFunc; /* The compare function used for insert */
}*QueueADT;
QueueADT que_create( int (*cmp)(const void*a,const void*b) ) {
struct QueueADT *new;
new = (struct QueueADT*)malloc(sizeof(struct QueueADT));
if (cmp == NULL) {
//do I create a new pointer to the cmp_int64?
new->front = NULL;
new->rear = NULL;
new->cmprFunc = NULL;
} else {
new->cmprFunc = &cmp;
new->front = NULL;
new->rear = NULL;
}
return ( new );
}
修改1 错误消息来自
queueADT.c:22:3: error: 'QueueADT' redeclared as different kind of symbol
}*QueueADT;
^
In file included from queueADT.c:9:0:
queueADT.h:23:21: note: previous declaration of 'QueueADT' was here
typedef struct { } *QueueADT;
^
queueADT.c:30:1: error: unknown type name 'QueueADT'
QueueADT que_create( int (*cmp)(const void*a,const void*b) ) {
^
到
In file included from queueADT.c:8:0:
queueADT.h:44:1: error: unknown type name 'QueueADT'
QueueADT que_create( int (*cmp)(const void*a,const void*b) );
^
queueADT.h:49:19: error: unknown type name 'QueueADT'
void que_destroy( QueueADT queue );
#define QUEUE_SIZE 5
#include "queueADT.h"
#define _QUEUE_IMPL_
struct node {
void* data;
//size_t num;
struct node *next;
}node;
typedef struct QueueADT {
struct node *front; /* pointer to front of queue */
struct node *rear; /* pointer to rear of queue */
//int *contents[ QUEUE_SIZE ]; /* number of items in queue */
int *cmprFunc; /* The compare function used for insert */
}*QueueADT;
QueueADT que_create( int (*cmp)(const void*a,const void*b) ) {
struct QueueADT *new;
new = (struct QueueADT*)malloc(sizeof(struct QueueADT));
if (cmp == NULL) {
//do I create a new pointer to the cmp_int64?
new->front = NULL;
new->rear = NULL;
new->cmprFunc = NULL;
} else {
new->cmprFunc = &cmp;
new->front = NULL;
new->rear = NULL;
}
return ( new );
}
答案 0 :(得分:0)
C档案的第一行
#ifndef _QUEUE_IMPL_
不正确,应该定义宏,而不是测试它:
#define _QUEUE_IMPL_
话虽如此,有一种更简单的方法可以完成你想要做的事情。您可以在标题中包含单行,而不是预处理程序包装:
typedef struct QueueADT *QueueADT;
即使没有定义struct QueueADT
(N.B。这是不同而不是QueueADT
),您仍然可以使用指向它的指针。然后,在C文件中,您只需定义struct QueueADT
:
struct QueueADT
{
...
};
请注意typedef
定义中缺少struct
。
答案 1 :(得分:0)
请注意每个文件顶部附近的#ifndef
?那就是问“如果没有定义这个符号,请包含这些内容;否则,请忽略它”。正如评论所说,这是为了防止重新定义事物。但是为了使它起作用,你必须在代码块中定义那个你不需要重复的定义的符号。
答案 2 :(得分:0)
<强> queueADT.h 强>
#ifndef _QUEUE_IMPL_
#define _QUEUE_IMPL_
#define QUEUE_SIZE 5
typedef struct node {
void* data;
//size_t num;
struct node *next;
} node;
typedef struct QueueADT {
struct node *front; /* pointer to front of queue */
struct node *rear; /* pointer to rear of queue */
int *contents[ QUEUE_SIZE ]; /* number of items in queue */
int *cmprFunc; /* The compare function used for insert */
} *QueueADT;
#endif
答案 3 :(得分:0)
没有找到queueStruct的原因没有被定义为QueueStruct的类型que_crear是因为它们没有按照下面列出的顺序定义
typedef struct node {
void* data;
struct node *next;
}node;
struct queueStruct {
struct node *front; /* pointer to front of queue */
struct node *rear; /* pointer to rear of queue */
int *cmprFunc;
size_t num; /* The compare function used for insert */
};
typedef struct queueStruct *QueueADT;
#define _QUEUE_IMPL_
#include "queueADT.h"
所以@Drew我确实需要单行标题,但必须在struct queueStruct之后和queueADT.h之前定义