错误:ISO C++禁止在没有类型的情况下声明类型名(-fpermissive)

编程语言 2026-07-08

我正在尝试编译以下代码:

#include <stdio.h>
#include <variant>

int main()
{   
    enum class TokenType {
        FOO,
        BAR,
    };

    using ConstTokenValue = const std::variant<std::string, int>;
    using ConstTokenType = const TokenType;

    return 0;
}

但我遇到了这两个错误:

 error: ISO C++ forbids declaration of ‘type name’ with no type [-fpermissive]
     using ConstTokenValue = const std::variant<std::string, int>;
                                        ^~~~~~~
 error: expected ‘;’
     using ConstTokenValue = const std::variant<std::string, int>;
                                  ^~~~
                                  ;

解决方案

你需要在代码中包含 <string>

生成的代码:

#include <stdio.h>
#include <variant>
#include <string>


int main()
{   
    enum class TokenType {
        FOO,
        BAR,
    };

    using ConstTokenValue = const std::variant<std::string, int>;
    using ConstTokenType = const TokenType;

    return 0;
}
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章