gpt4 book ai didi

c++ - 使用 std::map 的 Visual Studio 11 编译时错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:28:35 26 4
gpt4 key购买 nike

以下代码可以使用 gcc-4.5.1 编译,但不能在 Visual Studio 11 中编译。

#include <map>
#include <array>

typedef std::pair<const unsigned int, std::array<const unsigned int, 4>> pairus;

int main(){

std::map<const unsigned int, std::array<const unsigned int, 4> > x;
std::array<const unsigned int, 4> troll = {1, 2, 3, 4};
x.insert(pairus(1, troll));

auto z = x[1];
}

1 现在映射到 std::array<> troll .插入效果很好,程序可以编译。但是,一旦我尝试 auto z = x[1] -> 因此试图获得 1 的数组 troll被映射到,程序不编译并出现以下错误:

error C2512: 'std::array<_Ty,_Size>::array' : 没有合适的默认构造函数可用

是什么导致了 gcc 和 vs11 之间的这种行为差异以及如何解决它?

谢谢。

最佳答案

试试 auto z = *x.find(1); 代替。 [] 运算符需要一个默认可构造的类型。事实上,整个容器都需要一个默认可构造的类型,因此在尝试各种实现时,除了随机运气外,别无他法。

关于c++ - 使用 std::map 的 Visual Studio 11 编译时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10467042/

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