当我尝试编译此代码时,出现 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)

最佳答案

问题在于,当 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'.

所以你应该这样做。 (事实上​​,我知道您已经这样做了,因为在我发布此答案之前,我们在评论中讨论了这一点:-)。

关于visual-studio - Visual Studio 9.0 错误 C2051 大小写表达式不是常量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11461915/

10-11 22:40
查看更多