-6ren"> -这在 VS2018 中有效,但在 2008 中无效,我不确定如何修复它。 #include #include int main() { std::map myMap = { -6ren">
gpt4 book ai didi

c++ - 如何修复 "non-aggregates cannot be initialized with initializer list” < map >

转载 作者:行者123 更新时间:2023-11-27 22:33:34 27 4
gpt4 key购买 nike

这在 VS2018 中有效,但在 2008 中无效,我不确定如何修复它。

#include <map>
#include <string>

int main() {
std::map<std::string, std::string> myMap = {
{"Code", "Test"},
{"Code", "Test1"},
{"Code", "Test2"},
};
}

这是错误:错误 2 错误 C2552:“myMap”:无法使用初始化列表初始化非聚合

最佳答案

VS2008 是一个旧的编译器,不支持为此所需的 C++11。

你可以插入每个元素:

int main() {
std::map<std::string, std::string> myMap;

myMap["Code"] = "Test";
myMap["Code"] = "Test1";
myMap["Code"] = "Test2";
}

或者你可以使用 boost:

#include "boost/assign.hpp"

int main() {
std::map<std::string, std::string> myMap = boost::assign::map_list_of
("Code", "Test")
("Code", "Test1")
("Code", "Test2");
}

关于c++ - 如何修复 "non-aggregates cannot be initialized with initializer list” < map >,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57873894/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com