在 C++ 中初始化二维结构数组

发布于 2024-09-19 17:28:35 字数 1229 浏览 0 评论 0原文

我正在尝试用 C++ 初始化一个二维结构数组,但出现错误。有人可以告诉我我做错了什么吗?我重新检查了牙套,看起来没问题。

我的代码:


struct CornerRotationInfo {
  bool does_breed;
  int breed_slope;
  bool self_inversion;
  int self_slope;
  inline CornerRotationInfo(bool db, int bs, bool si, int ss) : does_breed(db), breed_slope(bs), self_inversion(si), self_slope(ss) {};
};

#define NO false
#define YES true
#define R 1
#define F -1
#define H 0
static const CornerRotationInfo corner_rot_info[3][8] = {
  // { 0, 45, 90, 135
  //  180, 225, 270, 315 }
  {
    { NO, F, NO, F }, {YES, F, NO, H }, {YES, H, NO, R}, {NO, R, YES, R },
    { NO, F, NO, F }, {YES, F, NO, H }, {YES, H, NO, R}, {NO, R, YES, R }
  }, // Falling
  {
    { NO, H, NO, H }, {YES, F, NO, R }, {NO, H, YES, H }, {YES, R, NO, F },
    { NO, H, NO, H }, {YES, F, NO, R }, {NO, H, YES, H }, {YES, R, NO, F }
  }, // Horizontal
  {
    { NO, R, NO, R }, {NO, F, YES, F }, {YES, H, NO, F}, {YES, R, NO, H },
    { NO, R, NO, R }, {NO, F, YES, F }, {YES, H, NO, F}, {YES, R, NO, H }
  }  // Rising
};

#undef NO
#undef YES
#undef R
#undef F
#undef H

我得到的错误是:

Transformation.C:72: error: brace-enclosed initializer used to initialize `const
 CornerRotationInfo'

I am trying to initialize a 2D array of structs in C++, but am getting an error. Can someone please tell me what am I doing wrong? I have rechecked the braces and they seem to be fine.

My code:


struct CornerRotationInfo {
  bool does_breed;
  int breed_slope;
  bool self_inversion;
  int self_slope;
  inline CornerRotationInfo(bool db, int bs, bool si, int ss) : does_breed(db), breed_slope(bs), self_inversion(si), self_slope(ss) {};
};

#define NO false
#define YES true
#define R 1
#define F -1
#define H 0
static const CornerRotationInfo corner_rot_info[3][8] = {
  // { 0, 45, 90, 135
  //  180, 225, 270, 315 }
  {
    { NO, F, NO, F }, {YES, F, NO, H }, {YES, H, NO, R}, {NO, R, YES, R },
    { NO, F, NO, F }, {YES, F, NO, H }, {YES, H, NO, R}, {NO, R, YES, R }
  }, // Falling
  {
    { NO, H, NO, H }, {YES, F, NO, R }, {NO, H, YES, H }, {YES, R, NO, F },
    { NO, H, NO, H }, {YES, F, NO, R }, {NO, H, YES, H }, {YES, R, NO, F }
  }, // Horizontal
  {
    { NO, R, NO, R }, {NO, F, YES, F }, {YES, H, NO, F}, {YES, R, NO, H },
    { NO, R, NO, R }, {NO, F, YES, F }, {YES, H, NO, F}, {YES, R, NO, H }
  }  // Rising
};

#undef NO
#undef YES
#undef R
#undef F
#undef H

The error I am getting is:

Transformation.C:72: error: brace-enclosed initializer used to initialize `const
 CornerRotationInfo'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

送君千里 2024-09-26 17:28:35

当您尝试使用聚合初始值设定项来初始化具有使用声明的构造函数的对象数组时,您可以使用的语法在很大程度上取决于单个元素的构造函数具有多少个参数。

如果构造函数只有(读:接受)一个参数,则可以使用“正常”聚合初始值设定项语法,如然而

std::string a[2] = { "abc", "def" };

,如果您想要(或拥有)使用的构造函数需要多个参数,则无法传递构造函数参数作为 {} 封闭的列表。您别无选择,只能在聚合初始值设定项中显式创建目标类型的临时对象,如

std::vector v[2] = { std::vector(10, 3), std::vector(8, 2) };

这正是您在您的情况下必须执行的操作

static const CornerRotationInfo corner_rot_info[3][8] = {
  {
    CornerRotationInfo(NO, F, NO, F), 
    CornerRotationInfo(YES, F, NO, H),
    ...
  },
  ...
};

等等。

请注意,在 C++ 中,由 {} 封闭的初始值设定项列表执行的初始化在概念上是复制初始化,这意味着您通过显式创建这些临时变量并不会真正进行太多更改。即,从概念上讲,临时变量始终是在聚合初始化期间创建的。无论如何,编译器通常都会优化它们。

When you are trying to use aggregate initializer to initialize an array of objects with used-declared constructor, the syntax you can use depends significantly on how many parameters the individual element's constructor has.

If the constructor has (read: accepts) only one parameter, you can use "normal" aggregate initializer syntax, as in

std::string a[2] = { "abc", "def" };

However, if the constructor you want (or have) to use requires more than one parameter, you cannot pass the constructor arguments as a {}-enclosed list. You have no other choice but to explicitly create temporary objects of the target type in the aggregate initializer, as in

std::vector v[2] = { std::vector(10, 3), std::vector(8, 2) };

This is exactly what you have to do in your case

static const CornerRotationInfo corner_rot_info[3][8] = {
  {
    CornerRotationInfo(NO, F, NO, F), 
    CornerRotationInfo(YES, F, NO, H),
    ...
  },
  ...
};

and so on.

Note that in C++ initialization performed by an {}-enclosed initializer list is conceptually a copy-initialization, which means that you are not really changing much by creating those temporaries explicitly. I.e. conceptually the temporaries are always created during aggregate initialization. The compiler will normally optimize them away anyway.

紫轩蝶泪 2024-09-26 17:28:35

如果我理解您的意图,问题是您已经给了 CornerRotationInfo 一个构造函数。这意味着它不再是聚合结构,并且您不能使用正常的聚合初始化。

如果删除构造函数,则用大括号括起来的初始值设定项应该可以工作。

(另一方面,如果您尝试使用 C++0x 的 initializer_list 您应该在问题中明确这一点。)

If I understand your intention the problem is that you have given CornerRotationInfo a constructor. This means that it is no longer an aggregate structure and you can't use normal aggregate initialization.

If you remove the constructor your brace-enclosed initializer should work.

(If, on the other hand, you are trying to use C++0x's initializer_list you should make this clear in your question.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文