C 递归结构类型定义
我正在创建以下结构指针类型:C 递归结构类型定义,c,struct,C,Struct,我正在创建以下结构指针类型: typedef struct hashmap_item { hashmap_item_t prev; hashmap_item_t next; char* key; void* value; int time_added; } *hashmap_item_t; 但我得到了以下错误: hashmap.h:5: error: expected specifier-qualifier-list before "hashmap_it
typedef struct hashmap_item {
hashmap_item_t prev;
hashmap_item_t next;
char* key;
void* value;
int time_added;
} *hashmap_item_t;
但我得到了以下错误:
hashmap.h:5: error: expected specifier-qualifier-list before "hashmap_item_t"
我假设这是因为我定义的结构本身包含一个字段。我怎样才能避免这种情况?有没有一种方法可以转发声明结构
谢谢 你不能那样做。。。你可以
// C,C++ allows pointers to incomplete types.
typedef struct hashmap_item *hashmap_item_t;
struct hashmap_item {
hashmap_item_t prev;
hashmap_item_t next;
char* key;
void* value;
int time_added;
}; // Till this point the structure is incomplete.
当编译器开始解析代码时,它会发现hashmap\u item\u t
以前没有在任何地方声明过。因此,它将抛出一条错误消息
typedef struct hashmap_item {
hashmap_item_t prev; // Compiler was unable to find 'hashmap_item_t'
hashmap_item_t next; // Compiler was unable to find 'hashmap_item_t'
char* key;
void* value;
int time_added;
} *hashmap_item_t;// But 'hashmap_item_t' identifier appears here!!!
你不能那样做。。。你可以
// C,C++ allows pointers to incomplete types.
typedef struct hashmap_item *hashmap_item_t;
struct hashmap_item {
hashmap_item_t prev;
hashmap_item_t next;
char* key;
void* value;
int time_added;
}; // Till this point the structure is incomplete.
当编译器开始解析代码时,它会发现hashmap\u item\u t
以前没有在任何地方声明过。因此,它将抛出一条错误消息
typedef struct hashmap_item {
hashmap_item_t prev; // Compiler was unable to find 'hashmap_item_t'
hashmap_item_t next; // Compiler was unable to find 'hashmap_item_t'
char* key;
void* value;
int time_added;
} *hashmap_item_t;// But 'hashmap_item_t' identifier appears here!!!
当编译器开始声明
prev
和next
成员时,它会尝试查找标识符hashmap\u item\t
,但它尚未声明。在C语言中,所有标识符都必须声明才能使用
您有两种选择:要么在结构之前声明typedef
(是的,它是合法的);或使用结构声明,例如:
typedef struct hashmap_item {
struct hashmap_item *prev;
struct hashmap_item *next;
char* key;
void* value;
int time_added;
} *hashmap_item_t;
当编译器开始声明
prev
和next
成员时,它会尝试查找标识符hashmap\u item\t
,但它尚未声明。在C语言中,所有标识符都必须声明才能使用
您有两种选择:要么在结构之前声明typedef
(是的,它是合法的);或使用结构声明,例如:
typedef struct hashmap_item {
struct hashmap_item *prev;
struct hashmap_item *next;
char* key;
void* value;
int time_added;
} *hashmap_item_t;
我希望结构包含hashmap\u item\t,它们是指向hashmaps的指针。。。将hashmap_item*作为字段是否是使其工作的唯一方法?我希望该结构包含hashmap_item,它们是指向hashmaps的指针。。。将hashmap_item*作为字段是使其工作的唯一方法吗?或者使用@Joachim Pileborg建议的结构声明,或者使用@Joachim Pileborg建议的结构声明