【问题标题】:error: expected identifier or ‘(’ before, macro definition错误:预期标识符或“(”之前,宏定义
【发布时间】:2012-06-08 18:18:58
【问题描述】:

我在尝试定义这个类似函数的宏时遇到了麻烦,它采用 4 个矢量幅度来表示一般圆柱体顶面的长轴和短轴,并确定一般圆柱体的类型。 EQUAL 是一个已定义的宏,用于查看两个浮点值是否“相等”。

3080 #define GET_TGC_TYPE(_type, _a, _b, _c, _d) { \
3081     if (EQUAL((_a), (_b)) && EQUAL((_c), (_d))) { \
3082         /* circular base and top */
3083         if (EQUAL((_a), (_c))) { \
3084             /* right circular cylinder */
3085             (_type) = RCC; \
3086         } else { \
3087             /* truncated right cone */
3088             (_type) = TRC; \
3089         } \
3090     } else { \
3091         /* elliptical base or top */
3092         if (EQUAL((_a), (_c)) && EQUAL((_b), (_d))) { \
3093             /* right elliptical cylinder */
3094             (_type) = REC; \
3095         } else { \
3096             /* truncated elliptical cone */
3097             (_type) = TEC; \
3098         } \
3099     }
3100 }

我遇到的错误是

3083:9: error: expected identifier or ‘(’ before ‘if’
3086:11: error: expected identifier or ‘(’ before ‘else’
3090:5: error: expected identifier or ‘(’ before ‘}’ token
3090:7: error: expected identifier or ‘(’ before ‘else’
3100:1: error: expected identifier or ‘(’ before ‘}’ token

我对 C 宏没有太多经验,所以我完全有可能遗漏了一些明显的东西。

【问题讨论】:

  • 作为一个不得不维护这样的宏的人,我礼貌地请求您抛弃整个实现并将其重写为适当的 C 函数。您可以将其设为静态内联并将其粘贴在头文件中,它的行为应该完全相同,但没有当前实现的任何潜在宏副作用。
  • @AndrewCottrell:我不反对,你能解释一下当前宏可能出现的一些问题吗?
  • @ChrisDueck:我相信您的原始帖子解释了当前宏可能出现的一个问题;-)
  • @ChrisDueck,刚刚出现了一个例子in a question today

标签: c c-preprocessor


【解决方案1】:

您的带有 cmets 的行不包含尾随 \,因此宏定义在它们的第一个处停止。

【讨论】:

    【解决方案2】:

    您的注释行中似乎缺少反斜杠。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多