給定以下代碼(它是一個基於所包含的類型爲列表數據結構生成代碼的宏)。C宏問題:重新定義函數/結構
list.h
#ifndef _LIST_H
#define _LIST_H
#ifdef __cplusplus
extern "C" {
#endif
#define LIST_TEMPLATE_INIT(type) \
typedef struct __list_s_##type { \
struct __list_s_##type *next; \
type value; \
} __list_##type; \
\
__list_##type * __list_##type##_malloc(type value){ \
__list_##type * list = NULL; \
list = malloc(sizeof(*list)); \
list->value = value; \
return list; \
}\
\
void __list_##type##_free(__list_##type *list){\
__list_##type * back = list;\
while(list=list->next){\
free(back);\
back = list;\
}\
}
#define LIST_TYPE(type) __list_##type
#define LIST_MALLOC(type,value) __list_##type##_malloc(value)
#define LIST_FREE(type,list) __list_##type##_free(list)
#define LIST_DATA(list) (list->value)
#ifdef __cplusplus
}
#endif
#endif /* _LIST_H */
這裏是上面的代碼是如何工作的:
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
/*
*
*/
LIST_TEMPLATE_INIT(int)
int main(int argc, char** argv)
{
LIST_TYPE(int)* list = NULL;
list = LIST_MALLOC(int, 5);
printf("%d",LIST_DATA(list));
LIST_FREE(int,list);
return (0);
}
我的問題,是它可能以某種方式能夠撥打電話:LIST_TEMPLATE_INIT(int)
,多次如我所願,以分散的方式?
當前的問題與現在這個權利是在另一個文件中加註編譯錯誤調用LIST_TEMPLATE_INIT(int)
(因爲功能重新定義):錯誤的
例子:
error: redefinition of ‘struct __list_s_int’
error: redefinition of ‘struct __list_s_int’
error: conflicting types for ‘__list_int’
note: previous declaration of ‘__list_int’ was here
error: conflicting types for ‘__list_int_malloc’
note: previous definition of ‘__list_int_malloc’ was here
error: conflicting types for ‘__list_int_free’
note: previous definition of ‘__list_int_free’ was here
外部的 「C」 是宏定義 – 2010-06-08 22:07:53
是由IDE生成的冗餘包裝。感謝您的評論,我會調查! – 2010-06-08 22:17:21