在C ++ 11中初始化结构的C ++ std :: array [英] initialise a C++ std::array of struct in C++11

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

问题描述

我已经读过类似的问题,但是没有人知道为什么我有一个结构

I've read the similar questions, but does anyone know why if I have a struct

struct ArabicRoman {
    char roman;
    int arabic;
};

我可以通过以下方式初始化C ++ std :: array :

I can initialise a C++ std::array in the following way:

ArabicRoman M({'M', 1000});
ArabicRoman D({'D', 500});
array<ArabicRoman, 2> const SYMBOLS({ M, D });

我可以通过以下方式初始化C样式数组:

I can initialise a C-style array in the following way:

ArabicRoman const SYMBOLS[]({ {'M', 1000}, {'D', 500} });

但是,以下内容无法编译:

However, the following is not compiling:

array<ArabicRoman, 2> const SYMBOLS({ {'M', 1000}, {'D', 500} });

是否有任何初始化C ++样式结构数组的解决方法?

any workaround to initialise C++ style arrays of structs?

推荐答案

您需要用大括号替换括号:

You need to replaces parentheses with braces:

std::array<ArabicRoman, 2> const SYMBOLS {{ {'M', 1000}, {'D', 500} }};
                                         ^                           ^

实时演示

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

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