我有以下源文件(test.c
):
#include <iostream>
enum ecodes { ENOKEY = -1, EDUPKEY = -2 };
int main()
{
return 0;
}
当我编译without -std=c++11
时,它编译得很好。
g++ test.c -o test
使用-std=c++11
编译时,会出现编译错误: g++ -std=c++11 test.c -o测试
test.c:3:16: error: expected identifier before numeric constant
enum ecodes { ENOKEY = -1, EDUPKEY = -2 };
^
test.c:3:16: error: expected â}â before numeric constant
test.c:3:16: error: expected unqualified-id before numeric constant
test.c:3:42: error: expected declaration before â}â token
enum ecodes { ENOKEY = -1, EDUPKEY = -2 };
使用的编译器是Linux上的GNU g++ 4.9.2。
bash-4.2$ g++ --version
g++ (GCC) 4.9.2 20150212 (Red Hat 4.9.2-6)
请帮帮忙。
发布于 2017-03-23 22:13:04
ENOKEY
是在<errno.h>
中定义的错误代码
#define ENOKEY 126 /* Required key not available */
在您的构建平台上(至少在指定了-std=c++11
时),<iostream>
可能正在对<errno.h>
执行#include
d操作,所以下面这行代码:
enum ecodes { ENOKEY = -1, EDUPKEY = -2 };
得到预处理,以:
enum ecodes { 126 = -1, EDUPKEY = -2 };
因此出现了错误。
注意:您最初的示例代码用INVALID
代替了ENOKEY
,因此没有其他人能够重现这个问题。
带回家的信息:在提出问题时,始终提供一个适当的MCVE,其中包含重现错误的实际代码,而不是您认为问题所在的近似值。
https://stackoverflow.com/questions/42953520
复制相似问题