简体   繁体   English

C ++ struct array初始化

[英]C++ struct array initialization

This is ok: 还行吧:

int vec_1[3] = {1,2,3};

so what's wrong with 那有什么不对

struct arrays{
  int x[3];
  int y[3];
  int z[3];
};
arrays vec_2;
vec_2.x = {1,2,3};

that gives 这给了
error: cannot convert '<brace-enclosed initializer list>' to 'int' in assignment 错误:无法在赋值时将'<brace-enclosed initializer list>'转换为'int'

I've read a lot of posts on this error but it's still not clear where the problem is. 我已经阅读了很多关于此错误的帖子,但目前还不清楚问题出在哪里。

The first is initialization. 首先是初始化。 The second is an attempt at assignment, but arrays aren't assignable. 第二种是尝试分配,但是数组不可分配。

You could do something like: 你可以这样做:

arrays vec_2 = {{1,2,3}, {3,4,5}, {4,5,6}};

If you only want to initialize vec_2.x, then you can just leave out the rest of the initializers: 如果您只想初始化vec_2.x,那么您可以省略其余的初始值设定项:

 arrays vec_2 = {1,2,3};

In this case, the rest of vec_2 will be initialized to contain zeros. 在这种情况下, vec_2的其余部分将被初始化为包含零。

While you have to include at least one set of braces around the initializers, you don't have to include the "inner" ones if you don't want to. 虽然您必须在初始化程序周围包含至少一组大括号,但如果您不想,则不必包含“内部”大括号。 Including them can give you a little extra flexibility though. 包括它们可以给你一点额外的灵活性。 For example, if you wanted to initialize the first two items in vec_2.x and the first one in vec_2.y, you could use: 例如,如果要初始化vec_2.x中的前两个项目和vec_2.y中的第一个项目,可以使用:

arrays vec_2 = {{1,2}, {3}};

In this case, you'll get vec_2 set as if you'd used {1, 2, 0, 3, 0, 0, 0, 0, 0}; 在这种情况下,您将设置vec_2 ,就好像您使用了{1, 2, 0, 3, 0, 0, 0, 0, 0}; vec_2 {1, 2, 0, 3, 0, 0, 0, 0, 0}; as the initializer. 作为初始化器。

This is assignment, not initialization: 这是赋值,而不是初始化:

arrays vec_2;
vec_2.x = {1,2,3}; 

Use the following, which is equivalent to what you were attempting: 使用以下内容,这相当于您尝试的内容:

arrays vec_2 = { {1, 2, 3 } };

If you want to provide values for y and z also: 如果您还想为yz提供值:

arrays vec_2 = { {1,2,3}, // x
                 {4,5,6}, // y
                 {7,8,9}  // z
               };
arrays vec_2;
vec_2.x = {1,2,3};

As the error message says, the second line is assignment , not initialization . 正如错误消息所示,第二行是赋值 ,而不是初始化 They're two different things. 他们是两件不同的事。

You need to do this: 你需要这样做:

arrays vec_2 = {
      {1,2,3}, //initializes x 
      {5,6,7}, //initializes y
      {7,8,9}, //initializes z 
};

Note that , is allowed after the last inner-brace! 需要注意的是,在最后的内梅开二度后不允许的! (ie {7,8,9}, <-- allowed). (即{7,8,9}, < - 允许)。 Such trailing comma is usually helpful for generated code: you can add another line, without caring about comma, neither do you need to consider any special case for the last line. 这样的尾随逗号通常对生成的代码有用:你可以添加另一行,而不用关心逗号,也不需要考虑最后一行的任何特殊情况。

int vec_1[3] = {1,2,3};

Despite what it looks like, this is not assignment , this is (effectively) a constructor call. 尽管它看起来像,但这不是赋值 ,这是(有效)构造函数调用。 It constructs a array of int initialized to those values. 它构造一个初始化为这些值的int数组。

arrays vec_2;

This constructs a struct, and each of the member arrays with default values. 这构造了一个struct,并且每个成员数组都有默认值。

vec_2.x = {1,2,3};

You cannot assign arrays, and this member has already been constructed. 您无法分配数组,并且已经构建了此成员。 The workaround is as such: 解决方法是这样的:

arrays vec_2 = { {1, 2, 3} };

which is the same thing as 这是一回事

arrays vec_2 = { {1, 2, 3}, {0, 0, 0}, {0, 0, 0} };

In the first example you are intializing the vector when you declare it. 在第一个示例中,您在声明向量时正在初始化向量。

In the second example, you are intializing twice (illegal), once when you declare the arrays struct and then again when you say vec_2.x = {1,2,3} 在第二个例子中,你初始化两次(非法),一次声明arrays struct ,然后当你说vec_2.x = {1,2,3}时再次vec_2.x = {1,2,3}

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

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