Struct Initialization

Hello below you will see that I tried to define a struct and then initialize some of its members and the program builds fine with no errors whatsoever however in my IDE (VS 2017) some of things are underlined show errors.I.E. myemt -> declaration has no storage class or type specifier. If anyone could advise me on where I am going wrong with my declaration that would be great! Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
 #include <iostream>
#include <string>

struct EMT
{
	int pipesize;
	float bendRadius;
	float shoeDimension;
	float conduitDimension;
	float tail;
};

EMT myemt[10][5]; // 10 rows by 5 columns

myemt[0][0].pipesize = 12;
myemt[0][1].bendRadius = 5.883;
myemt[0][2].shoeDimension = 2.973;
myemt[0][3].conduitDimension = 0.530;
myemt[0][4].tail = 48.000;

myemt[1][0].pipesize = 34;
myemt[1][1].bendRadius = 6.000;
myemt[1][2].shoeDimension = 2.480;
myemt[1][3].conduitDimension = 0.692;
myemt[1][4].tail = 48.000;

myemt[2][0].pipesize = 100;
myemt[2][1].bendRadius = 7.000;
myemt[2][2].shoeDimension = 2.990;
myemt[2][3].conduitDimension = 0.872;
myemt[2][4].tail = 48.000;

myemt[3][0].pipesize = 114;
myemt[3][1].bendRadius = 8.500;
myemt[3][2].shoeDimension = 3.662;
myemt[3][3].conduitDimension = 1.133;
myemt[3][4].tail = 46.500;

myemt[4][0].pipesize = 112;
myemt[4][1].bendRadius = 8.500;
myemt[4][2].shoeDimension = 3.662;
myemt[4][3].conduitDimension = 1.305;
myemt[4][4].tail = 46.500;

myemt[5][0].pipesize = 200;
myemt[5][1].bendRadius = 9.250;
myemt[5][2].shoeDimension = 3.610;
myemt[5][3].conduitDimension = 1.648;
myemt[5][4].tail = 46.500;

myemt[6][0].pipesize = 212;
myemt[6][1].bendRadius = 12.000;
myemt[6][2].shoeDimension = 4.180;
myemt[6][3].conduitDimension = 2.156;
myemt[6][4].tail = 46.500;

myemt[7][0].pipesize = 300;
myemt[7][1].bendRadius = 16.000;
myemt[7][2].shoeDimension = 7.544;
myemt[7][3].conduitDimension = 2.625;
myemt[7][4].tail = 53.750;

myemt[8][0].pipesize = 312;
myemt[8][1].bendRadius = 18.625;
myemt[8][2].shoeDimension = 7.518;
myemt[8][3].conduitDimension = 3.000;
myemt[8][4].tail = 53.750;

myemt[9][0].pipesize = 400;
myemt[9][1].bendRadius = 20.875;
myemt[9][2].shoeDimension = 7.277;
myemt[9][3].conduitDimension = 3.375;
myemt[9][4].tail = 53.750;
Either put all that initialization stuff in a function and call it, or simply do it like this:

1
2
3
4
EMT myemt[10][5] {
    {12, 5.883,  2.973, 0.530, 48.000},
    ...
};

You can't initialize your array outside a function.
You can initialize it when you declare it:
1
2
3
4
5
6
EMT myemt[10][5] = 
{
  {12, 5.883, 2.973, 0.530, 48.000},
  // and so on
};


Thank you both! However how do I now specifify which member it is? I.E. pipesize, bendradius etc?
Last edited on
If you don't specify it will use the order that the members are defined in the struct.

1
2
3
4
5
6
7
8
9
10
11
EMT myemt[10][5] = 
{
	{
		12,    // pipesize
		5.883, // bendRadius
		2.973, // shoeDimension
		0.530, // conduitDimension
		48.000 // tail
	},
	// and so on
};

In C++20 you will be able to specify which members you want to initialize explicitly, but the order must still be the same as they are defined inside the struct.

1
2
3
4
5
6
7
8
9
10
11
EMT myemt[10][5] = 
{
	{
		.pipesize = 12,
		.bendRadius = 5.883,
		.shoeDimension = 2.973,
		.conduitDimension = 0.530,
		.tail = 48.000
	},
	// and so on
};
Topic archived. No new replies allowed.