我有以下代码,当我试图编译它时,我得到一个错误:
错误:' list_item_t '没有名为' state '的成员
有什么创造性的想法如何使这段代码编译没有警告和错误?
#if defined (_DEBUG_)
#define ASSERT assert
#else /* _DEBUG_ */
#define ASSERT( exp ) ((void)(exp))
#endif`
typedef struct list_item {
struct list_item *p_next;
struct list_item *p_prev;
#ifdef _DEBUG_
int state;
#endif
} list_item_t;
main(int argc, char *argv)
{
list_item_t p_list_item;
ASSERT(p_list_item.state == 0);
}
只是#define
ASSERT
#if defined (_DEBUG_)
#define ASSERT assert
#else
#define ASSERT( exp ) (void)0
#endif
注意,这可能会改变其他代码点的行为,因为ASSERT
不再计算它的参数,但这是人们期望它如何表现的。
或执行_DEBUG_
构建,但这并不能解决问题,它只是避免它。
当且仅当定义了_DEBUG_
时,您的类具有上述成员,而显然没有。
#define _DEBUG_
放在TU的开头,或者更改项目设置以其他方式定义
这是因为
#define ASSERT( exp ) ((void)(exp))
计算p_list_item.state == 0
,因此即使_DEBUG_
不是#define
'd,也需要state
存在。