【发布时间】:2017-08-14 16:16:00
【问题描述】:
我有以下源文件 (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
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)
请帮忙。
【问题讨论】:
-
C++11用于 C 程序?你确定?严重的是,C++03、C++11 和 C 在enums 的实现上有所不同,所以你需要澄清一下。 -
使用 g++ 4.9.2 对我来说可以编译 - 我猜某处的标题中存在名称冲突(宏?)。我想我们需要一个minimal reproducible example...
-
@Bathsheba 我已经从一个我面临问题的巨大代码库中放了一个最小的例子。代码库主要是 C++,但有一些 .c 文件以及给出的示例
-
@PaulR 同意。但是当我将文件编译为 g++ src.c 时,它可以编译,但是当我编译为 g++ -std=c++11 src.c 时,它会出现编译错误。如果这是由于名称冲突(我也这么认为),那不也是 g++ src.c 的原因吗?
-
我认为我们需要查看显示问题的最小代码体。上面的枚举是 C 和 C++ 中的有效语句,所以目前任何人都可以猜测问题是什么。
标签: linux c++11 enums compiler-errors g++4.9