You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 22.04下GCC编译大括号初始化列表报错求助

编译错误解决:Ubuntu 22.04下unordered_map大括号初始化失败

你的代码存在语法笔误,这是导致Ubuntu 22.04下g++编译失败的直接原因:

在road_type_string_map的初始化列表中,最后一行的RoadType:primary使用了单冒号:,正确的枚举成员访问符应该是双冒号::。

错误代码片段

enum RoadType { unclassified, service, residential, tertiary, secondary, primary };
static unordered_map<string,RoadType> const road_type_string_map = {
  { "unclassified", RoadType::unclassified },
  { "service", RoadType::service },
  { "residential", RoadType::residential },
  { "tertiary", RoadType::tertiary },
  { "secondary", RoadType::secondary },
  { "primary", RoadType:primary } // 此处为单冒号,属于语法错误
};

修正后的代码

enum RoadType { unclassified, service, residential, tertiary, secondary, primary };
static unordered_map<string,RoadType> const road_type_string_map = {
  { "unclassified", RoadType::unclassified },
  { "service", RoadType::service },
  { "residential", RoadType::residential },
  { "tertiary", RoadType::tertiary },
  { "secondary", RoadType::secondary },
  { "primary", RoadType::primary } // 修正为标准双冒号
};

补充说明

Ubuntu 20.04默认的g版本(9.x)对这类语法错误的检查相对宽松,允许部分非标准写法通过编译;而Ubuntu 22.04的g版本(11.x)严格遵循C++17标准,会直接抛出初始化列表转换错误。同一头文件中DriversSide相关代码能正常编译,是因为它不存在同类语法笔误。

内容的提问来源于stack exchange,提问作者alexbk66

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 14:54:25