初始化嵌套结构数组
我正在尝试初始化一个嵌套结构数组。
这就是我正在做的事情:
// init a nested struct
struct L1 {
struct L2 {
int i[4];
} l2[3];
};
L1::L2 l2 = {1,2,3,4};
L1::L2 l2_a[] = { {1,2,3}, {1,2}, {1,2,3,4}};
L1 l1 = {
{{1,2,3}, {1,2}, {1,2,3,4}}
};
L1 l1_a0 = {};
L1 l1_a1 = {0};
L1 l1_a[] = {
{{1,2,3}, {1,2}, {1,2,3,4}},
{{1,2,3}, {1,2}, {1,2,3,4}}
}; // ... error: too many initializers for 'L1'
根据上面发生的正确情况,我希望派生的最后一行(初始化嵌套结构数组)是正确的,但令我惊讶的是,编译器不喜欢它。
到底能做到吗? 这是有关初始化嵌套结构的相关文章。顺便说一句,我正在使用 g++。
I'm trying to initialize a nested structure array.
Here is what I am doing:
// init a nested struct
struct L1 {
struct L2 {
int i[4];
} l2[3];
};
L1::L2 l2 = {1,2,3,4};
L1::L2 l2_a[] = { {1,2,3}, {1,2}, {1,2,3,4}};
L1 l1 = {
{{1,2,3}, {1,2}, {1,2,3,4}}
};
L1 l1_a0 = {};
L1 l1_a1 = {0};
L1 l1_a[] = {
{{1,2,3}, {1,2}, {1,2,3,4}},
{{1,2,3}, {1,2}, {1,2,3,4}}
}; // ... error: too many initializers for 'L1'
According to what happens correct above, I would expect the the last line derived (initializing the array of nested struct) to be right, but to my surprise, the compiler doesn't like it.
Can it be done at all? Here's a related post about initializing nested structs. Btw, I'm using g++.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,你可以:始终编译
-Wall
的另一个原因,因为编译器可以给你有用的警告,例如:最终结果并不漂亮,但最终您在编译器输出的电传打字机硬拷贝上浪费了更少的树:
请在此处查看它:http://ideone.com/YKq3d
Yes you can: another reason to always compile
-Wall
, since the compiler could give you helpful warnings like:The net result isn't pretty, but you end up wasting fewer trees on your teletype hardcopy of the compiler output:
See it live here: http://ideone.com/YKq3d