初始化结构的std :: array [英] Initializing std::array of struct

查看:184
本文介绍了初始化结构的std :: array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.
我尝试使用Visual Studio 2010在C ++程序中将某些内部数组转换为std :: arrays.

难怪这个可以编译.

Hello everyone.
I tried to convert some intrinsic arrays into std::arrays in a C++ program with Visual Studio 2010.

No wonder this one could compile.

typedef struct {
    int a;
    int b;
} A;

A arr[2] = {
    { 0, 1 },
    { 2, 3 }
};


但是这个不能编译.它导致错误错误C2078:初始化程序太多".


But this one couldn''t compile. It caused an error "error C2078: too many initializers".

std::array<A, 2> arr = {
    { 0, 1 },
    { 2, 3 }
};


经过一段时间的尝试和错误,我发现该程序可以编译.
(添加了两个花括号.)


After trial and error for a while, I found that this one could compile.
(A couple of curly braces were added.)

std::array<A, 2> arr = {{
    { 0, 1 },
    { 2, 3 }
}};


是什么原因或原因?请有人解释.


What is the cause or the reason? Somebody please explain it.

推荐答案

我会使用
A local_element;
local_element.a = 0;
local_element.b = 1;
arr.push(local_element);

local_element.a = 2;
local_element.b = 3;
arr.push(local_element);









or

const std::array<a,> arr = {    A( 0, 1 ),    A( 2, 3 )};



如果要编写构造函数.



if you will write the constructor.


这篇关于初始化结构的std :: array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆