has no member compilation error

无人久伴 提交于 2019-12-11 07:32:31

问题


I have the following code and when I'm trying to compile it, I get an error:

error: ‘list_item_t’ has no member named ‘state’

Any creative ideas how to make this piece of code compile without warnings and erros?

 #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);
}

回答1:


Just #define ASSERT as

 #if defined (_DEBUG_)
 #define ASSERT       assert
 #else                          
 #define ASSERT( exp ) (void)0
 #endif

Note that this may change the behaviour of other code spots because ASSERT no longer evaluates its argument, but that's how people expect it to behave anyway.

Or perform a _DEBUG_ build, but this doesn't resolve the problem, it just avoids it.




回答2:


Your class has the mentioned member if and only if _DEBUG_ is defined, and it apparently isn't.

#define _DEBUG_

in the beginning of your TU or change project settings to define it in some other way




回答3:


This is due to

#define ASSERT( exp ) ((void)(exp))

which evaluates p_list_item.state == 0 and thus needs state to exist even when _DEBUG_ is not #define'd.



来源:https://stackoverflow.com/questions/6641538/has-no-member-compilation-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!