栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

error: expected expression before ‘struct‘

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

error: expected expression before ‘struct‘

一、编译报error
main.c:38:25: error: expected expression before ‘memory_node_t’
   cur = container_of(n, memory_node_t, rbtree_node);
                         ^
和参数‘memory_node_t’有关,展开container_of宏看看。


二、展开container_of宏
#define container_of(ptr, type, member) ({            
        char *__mptr = (void *)(ptr);                
        ((type *)(__mptr - offsetof(type, member))); })
可以发现,与‘memory_node_t’有关的表达式就两个:1.(type *);  2.offsetof(type, member)
1.(type *):判断是不会有问题的
2.offsetof(type, member):问题应该出在这里

三、继续往下看offsetof的定义
找到问题原因,offsetof宏在stddef.h中定义,#include即可

四、总结

1、Linux C/C++编程时常会遇到“error: expected expression before ‘struct’”错误,此错误一般是由未定义的宏(宏里套宏)或参量引起,导致编译器判断当前语句为非法语句。

2、解决编译错误,一定要从第一个error或者warning开始看起——比如上面这个问题,完整的编译log是这样的:

main.c: In function ‘memory_node_find_by_address’:
main.c:17:36: warning: implicit declaration of function ‘offsetof’ [-Wimplicit-function-declaration]
 #define container_of(type, member) offsetof(type, member)
                                    ^
main.c:36:9: note: in expansion of macro ‘container_of’
   cur = container_of(memory_node_t, rbtree_node);
         ^
main.c:36:22: error: expected expression before ‘memory_node_t’
   cur = container_of(memory_node_t, rbtree_node);
                      ^
main.c:17:45: note: in definition of macro ‘container_of’
 #define container_of(type, member) offsetof(type, member)
                                             ^
main.c:37:9: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
   start = cur->start;
         ^

第一个warning已经告诉我们了,offsetof未定义。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/443891.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号