今天写程序时碰到这个问题:

包含了第三方库的头文件,头文件内有如下声明:extern const int ZOO_CREATED_EVENT;

变量在.c文件内定义,已被编译到第三方库中。

然后我在switch逻辑中用了上面的常量:

switch (type) {
case ZOO_CREATED_EVENT:
    ...
    break;
}

编译报错如下,‘ZOO_CREATED_EVENT’ cannot appear in a constant-expression

比较诧异!

经过查阅了解到,编译器构造switch查找表时需要明确其常量值,而extern常量在编译时尚不能明确,所以会报错。

暂时使用"if ... else if ..."解决问题。

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-16
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-05
  • 2022-12-23
  • 2022-01-27
  • 2021-12-04
相关资源
相似解决方案