显示引入 #include <cstdint>
报错:
✗ cpp11-06-scoped-enums-1
- → \dslings\en\cpp11\06-scoped-enums-1.cpp
[ 23%]: cache compiling.release dslings\en\cpp11\06-scoped-enums-1.cpp
error: dslings\en\cpp11\06-scoped-enums-1.cpp:52:26: error: found ':' in nested-name-specifier, expected '::'
52 | enum class Color8Bit : int8_t {
| ^
| ::
dslings\en\cpp11\06-scoped-enums-1.cpp:52:16: error: use of enum 'Color8Bit' without previous declaration
52 | enum class Color8Bit : int8_t {
| ^~~~~~~~~
dslings\en\cpp11\06-scoped-enums-1.cpp:52:35: error: qualified-id in declaration before '{' token
52 | enum class Color8Bit : int8_t {
| ^
In file included from dslings\en\cpp11\06-scoped-enums-1.cpp:17:
dslings\en\cpp11\06-scoped-enums-1.cpp:60:26: error: 'Color8Bit' was not declared in this scope
60 | d2x_assert_eq(sizeof(Color8Bit), sizeof(int8_t)); // Can customize type int8_t
| ^~~~~~~~~
./d2x/cpp/common.hpp:20:9: note: in definition of macro 'd2x_assert_eq'
20 | if (a != b) {
| ^
in dslings\en\cpp11\06-scoped-enums-1.cpp error: exec(xmake build cpp11-06-scoped-enums-1) failed(-1), unknown reason
✨: AI Disabled / 未启用 - https://github.com/d2learn/d2x
操作系统:Windows 编译器:MinGW-W64 GCC 15.1.0 标准库:libstdc++(GCC 自带的 C++ 标准库) 核心原因 MinGW GCC 虽然跑在 Windows 上,但它用的是 libstdc++,不是 MSVC 的 STL。
在 libstdc++ 中,
为什么报错信息那么怪? GCC 解析到:
因为 int8_t 未定义,GCC 的解析器陷入了一个尴尬的中间状态——它没法把 int8_t 识别为类型,又试图把后面的东西按嵌套名称说明符(::)来解析,所以才会冒出 "found ':' in nested-name-specifier, expected '::'" 这种让人摸不着头脑的错误信息。
为什么如果换成 MSVC 可能不报错?
MSVC STL 的内部实现链条更长:
一句话总结
MinGW 用的是 libstdc++,它的
你好,上面是06-scoped-enums-1.cpp 题目报的错。我在linux上编译时通过的,换到windows上用其他编译器就会报错,并且错误会让新手摸不着头脑。我用deepseek建议我在头文件common.h 引入#include
'int f' is not const
这个是一个练习故意留的错误, 需要你修复 (编译期计算f不能是运行时变量)
具体可以参考, 下面两个链接
- https://github.com/mcpp-community/d2mcpp/blob/main/solutions/cpp11/07-constexpr-1.cpp
- https://www.bilibili.com/video/BV15CMEzLEuN
贴错了,应该是这道题:06-scoped-enums-1.cpp
贴错了,应该是这道题:06-scoped-enums-1.cpp
可能需要加 std::int8_t 有些编译器, 你可以贴具体报错。如果你用std::init8_t ok 你可以改一下这个参考答案 PR 一下
- https://github.com/mcpp-community/d2mcpp/blob/main/solutions/cpp11/06-scoped-enums-1.cpp