请教一个__LINE__ 的问题,vs报错

古么宁 2018-04-25 12:32:18
最近仿写protothread的时候,用到了 case __LINE__ :xxxx;但是vs2017和2008都编译报错,说__LINE__不是常量。然而我在其他的一些编译器上是可以正常编译通过的,困惑不已,请问有哪位大哥知道怎么回事?望指点一下迷津
...全文
675 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wallesyoyo 2018-04-26
  • 打赏
  • 举报
回复 1
https://stackoverflow.com/questions/11461915/visual-studio-9-0-error-c2051-case-expression-not-constant

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'.



如文中描述,把这一项换成只要不是 “Program Database for Edit and Continue” 就行。
古么宁 2018-04-26
  • 打赏
  • 举报
回复
引用 6 楼 jianwen0529 的回复:
编译器警告级别的问题?
这个我也调过了,但是不管用,比较尴尬
古么宁 2018-04-26
  • 打赏
  • 举报
回复
引用 5 楼 zilaishuichina 的回复:
引用 3 楼 yangxiangzhan 的回复:
vs2017实测没有报错, 输出 case 8
说的不错,就是这样的,但是我这边报错了,让我十分困惑。不知道是要设置预定义还是怎么做
那编译器具体的报错信息是啥啊[/quote]报错代码C2051,case 表达式不是常量
古么宁 2018-04-26
  • 打赏
  • 举报
回复
引用 5 楼 zilaishuichina 的回复:
引用 3 楼 yangxiangzhan 的回复:
vs2017实测没有报错, 输出 case 8
说的不错,就是这样的,但是我这边报错了,让我十分困惑。不知道是要设置预定义还是怎么做
那编译器具体的报错信息是啥啊[/quote]报错代码C2051,case 表达式不是常量.
古么宁 2018-04-26
  • 打赏
  • 举报
回复
引用 10 楼 wanggui2015 的回复:
https://stackoverflow.com/questions/11461915/visual-studio-9-0-error-c2051-case-expression-not-constant 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'. 如文中描述,把这一项换成只要不是 “Program Database for Edit and Continue” 就行。
膜拜!我找了好久不知道在哪改这个!跪谢!
幻夢之葉 2018-04-25
  • 打赏
  • 举报
回复
编译器警告级别的问题?
zilaishuichina 2018-04-25
  • 打赏
  • 举报
回复
引用 3 楼 yangxiangzhan 的回复:
vs2017实测没有报错, 输出 case 8
说的不错,就是这样的,但是我这边报错了,让我十分困惑。不知道是要设置预定义还是怎么做[/quote] 那编译器具体的报错信息是啥啊
古么宁 2018-04-25
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
婴儿出生时是不能case没投胎在大富大贵之家,就重新投胎的!
这位老师,这个 protothread 的写法我在其他平台运作的很好,只是挪到vs这就报错了
古么宁 2018-04-25
  • 打赏
  • 举报
回复
引用 1 楼 zilaishuichina 的回复:
你如果说的是类似如下的代码:

#include "stdafx.h"

int main(int argc, char* argv[])
{
	int a = 8;
	switch (a)
	{
		case __LINE__:
		printf("case %d", a);
		break;
		default:
		printf("default %d", a);
		break;
	}
	return 0;
}
vs2017实测没有报错, 输出 case 8
引用 1 楼 zilaishuichina 的回复:
你如果说的是类似如下的代码:

#include "stdafx.h"

int main(int argc, char* argv[])
{
	int a = 8;
	switch (a)
	{
		case __LINE__:
		printf("case %d", a);
		break;
		default:
		printf("default %d", a);
		break;
	}
	return 0;
}
vs2017实测没有报错, 输出 case 8
说的不错,就是这样的,但是我这边报错了,让我十分困惑。不知道是要设置预定义还是怎么做
赵4老师 2018-04-25
  • 打赏
  • 举报
回复 1
婴儿出生时是不能case没投胎在大富大贵之家,就重新投胎的!
zilaishuichina 2018-04-25
  • 打赏
  • 举报
回复
你如果说的是类似如下的代码:

#include "stdafx.h"

int main(int argc, char* argv[])
{
	int a = 8;
	switch (a)
	{
		case __LINE__:
		printf("case %d", a);
		break;
		default:
		printf("default %d", a);
		break;
	}
	return 0;
}
vs2017实测没有报错, 输出 case 8

70,012

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧