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
相关产品推荐
相关产品推荐

