Skip to content

显示引入 #include <cstdint> #83

Description

@linyihai

报错:

✗ 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++ 中, → → → 这条传递包含链到此为止了,不会继续往下传递包含 。所以 int8_t 根本没有被引入你的翻译单元,编译器完全不知道它是什么。

为什么报错信息那么怪?
GCC 解析到:

因为 int8_t 未定义,GCC 的解析器陷入了一个尴尬的中间状态——它没法把 int8_t 识别为类型,又试图把后面的东西按嵌套名称说明符(::)来解析,所以才会冒出 "found ':' in nested-name-specifier, expected '::'" 这种让人摸不着头脑的错误信息。

为什么如果换成 MSVC 可能不报错?
MSVC STL 的内部实现链条更长: → ... → → 内部实现 → 。所以 MSVC 下碰巧能编译通过。但这是实现细节,不是标准保证的,换个 MSVC 版本可能就失效了。

一句话总结
MinGW 用的是 libstdc++,它的 不会顺带把 拉进来,而你的练习文件又没显式 #include ,所以 int8_t 未定义导致编译失败。这是标准要求的行为,不是编译器 bug。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions