简体   繁体   English

全局声明二维数组(错误:初始值设定项太多)

[英]declaring 2d array globally (Error: too many initializer values)

I have used stackoverflow for a long time and never had to post a question before, but this one has me EXTREMELY frustrated.我使用 stackoverflow 已经很长时间了,以前从未发布过问题,但是这个问题让我非常沮丧。

I have this piece of code我有这段代码

array<array<string, 10>, 10> cShots = 
{
    { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
    { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
    { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
    { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
    { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
    { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
    { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
    { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
    { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
    { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " }
}; 

outside of any methods at a global level.在全球范围内的任何方法之外。 What I'm trying to do is create a 2d array 10 by 10 full of blank space strings.我想要做的是创建一个 10 x 10 的二维数组,其中包含空格字符串。 What happens instead is that I get an error on the first bracket of the third line saying "too many initializer values".相反,我在第三行的第一个括号上收到错误消息,说“初始值设定项太多”。 Every solution I have found to this error says that what I am doing is correct.我找到的针对此错误的每个解决方案都表明我正在做的事情是正确的。 I have tried doing it multiple other ways.我尝试过多种其他方式。

string** cShots = 
{
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "}
}; 

string cShots[10][10] = 
{
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
    {" ", " ", " ", " ", " ", " ", " ", " ", " ", " "}
}; 

but everything I try has the same result.但我尝试的每件事都有相同的结果。

PLEASE HELP!请帮忙!

EDIT: well...I figured out that I just needed more brackets...and the error went away...but now when I run my code visual studio says that it was a successful build, but then freezes forever...编辑:嗯......我发现我只需要更多的括号......错误就消失了......但是现在当我运行我的代码visual studio时说它是一个成功的构建,但随后永远冻结......

Things are a little easier to understand when you see that there is a special case for the syntax here.当您看到此处的语法有一个特殊情况时,事情就更容易理解了。 std::array is an aggregate, it's a struct that contains a single data member which is the underlying raw array. std::array是一个聚合,它是一个包含单个数据成员的结构,它是底层原始数组。 To initialize one of these you need to uniform-initialize the array (first braces) then initialize the underlying raw array member stored in the std::array (second braces);要初始化其中一个,您需要统一初始化数组(第一个大括号),然后初始化存储在std::array的底层原始数组成员(第二个大括号);

So when you have something such as:所以当你有这样的事情时:

array<string, 3> test = {{"a", "b", "c"}};

The outermost braces initialize the array itself then the inner part initializes the underlying array.最外面的大括号初始化array本身,然后内部部分初始化底层数组。 In this case it's clear that this is the way it is working and a special case was made (See the standard: section 8.5 for more) that allows the compiler to accept the other syntax of:在这种情况下,很明显这是它的工作方式,并且做了一个特殊情况(参见标准:第 8.5 节了解更多信息),允许编译器接受以下其他语法:

array<string, 3> test = {"a", "b", "c"} ;

However in the case of the question it's not of the special-case-form specified in the standard so you need the extra braces (this too caused me confusion when I first encountered it).但是,在问题的情况下,它不是标准中指定的特殊形式,因此您需要额外的大括号(这也让我第一次遇到它时感到困惑)。 There is a defect report about this actually http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2012/n3367.html#1270实际上有一个关于这个的缺陷报告http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2012/n3367.html#1270

In more recent c++ standards (see Can we omit the double-braces for std::array in C++14? ) this requirement is relaxed.在最近的 C++ 标准中(参见Can we omit the double-braces for std::array in C++14? ),这个要求被放宽了。

Because the code in the question does not benefit for the special case of the syntax we need both braces.因为问题中的代码对语法的特殊情况没有好处,所以我们需要两个大括号。 In this case your initializer is however not of length one:在这种情况下,您的初始化程序的长度不是一:

array<array<string, 10>, 10> cShots = 
{
    { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
    { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " }, //extra initializer elements but only one underlying raw array
    //snip
    { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " }
}; 

To the compiler it looks like you are trying to initialize multiple members of the std::array class but the problem is that there is only one underlying storage array.对于编译器来说,您似乎正在尝试初始化std::array类的多个成员,但问题是只有一个底层存储数组。 Hence the compiler error message you get here.因此,您会在此处获得编译器错误消息。 To get this to work you need to be more explicit and add the extra pair of braces to get an initializer of length one passed to the outer std::array .要使其工作,您需要更加明确并添加额外的一对大括号以获取传递给外部std::array的长度为 1 的初始值设定项。 You then initialize the underlying raw array which contains the other arrays:然后初始化包含其他数组的底层原始数组:

array<array<string, 10>, 10> cShots = 
{{
    { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
    { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " },
    //snip
    { " ", " ", " ", " ", " ", " ", " ", " ", " ", " " }
}}; 

For a proof of concept see this: http://coliru.stacked-crooked.com/a/4c57a7193629931f (Note the nested arrays here do benefit from the syntatical special case)有关概念证明,请参阅: http : //coliru.stacked-crooked.com/a/4c57a7193629931f (注意这里的嵌套数组确实受益于语法特殊情况)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM