Visual Studio 9.0错误C2051案例表达式不是常量

时间:2012-07-12 22:56:37

标签: visual-studio compiler-errors

当我尝试编译此代码时,出现Case Expression Not Constant错误。我无法弄清楚原因。

while ((*datalen) == 0)
    crReturn(NULL);  //error here
st->len = (st->len << 8) + **data;

函数crReturn()定义如下。

#define crReturn(z) \
do {\
    *crLine =__LINE__; return (z); case __LINE__:;\
} while (0)

1 个答案:

答案 0 :(得分:10)

问题在于MSVC ++在配置为其“编辑并继续”功能生成调试信息时执行非标准(与其自身文档相反)的操作,并且此非标准打破了__LINE__的使用方式在Simon Tatham的协程宏中。

以下是PuTTY源代码中的注释对此的说法:

In particular, if you are getting `case expression not constant'
errors when building with MS Visual Studio, this is because MS's
Edit and Continue debugging feature causes their compiler to
violate ANSI C. To disable Edit and Continue debugging:

- right-click ssh.c in the FileView
- click Settings
- select the C/C++ tab and the General category
- under `Debug info:', select anything _other_ than `Program
Database for Edit and Continue'.

所以你应该这样做。 (事实上​​,我知道你已经这样做了,因为我在发表这个答案之前在评论中对此进行了讨论: - )。)