【发布时间】:2022-11-03 19:58:55
【问题描述】:
MFC初学者在这里。
我试图像这样初始化std::map:(在CView的标题中)
// myprogramView.h
std::map<int, CStatic> myMap = {{10,{}}, {11,{}}};
但是编译器抱怨“没有构造函数的实例......与参数列表匹配”。
(编辑以供将来参考)上述消息是来自 IntelliSense 的错误。编译器(MSVC)说:C2664 'std::map<int,CStatic,std::less,std::allocator<std::pair<const _Kty,_Ty>>>::map(std::initializer_list<std ::pair<const _Kty,_Ty>>)':无法将参数 1 从 'initializer list' 转换为 'std::initializer_list<std::pair<const _Kty,_Ty>>'
但是,我们可以进行以下类型的初始化:
std::map<int, std::string> myMap2 = { {10,{}}, {11,{}} };
std::map<int, std::map<std::string, int>> myMap3 = { {10,{}}, {11,{}} };
为什么第一个示例无法编译,如何使用包含 MFC 对象的映射?
我试图在运行时或OnCreate 中访问地图中的控制对象和.Create()。
我也试过CMap,但似乎出现了同样的问题。
【问题讨论】:
-
请发布整个错误消息。
-
想要使用最终不会被使用的值来初始化
std::map似乎是不寻常的。是什么真实的你想在这里解决的问题?