Primitives using Structures

1
2
3
4
5
6
7
8
9
10
11
12
13
struct eVertex
{
	float X, Y, Z;
};

eVertex eVertices[3000];
uint eVertices_count = 0;

void primitive_E_vertex(float x, float y, float z)
{
	eVertices[eVertices_count] = {x, y, z};
	eVertices_count ++;
}


Anything wrong here? The compiling error is: syntax-error: '{' at this line:
eVertices[eVertices_count] = {x, y, z};
I don't really ever use structs, so I may be off, but perhaps you can only use the { } syntax to initialize a struct, not change it later? You could always just make a struct instance, populate it with the x, y, and z, and then assign the temporary to the array.
Something like this should work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct eVertex
{
	float X, Y, Z;

	// add a constructor to make initialising easier
	eVertex(float x, float y, float z): X(x),Y(y),Z(z){}
};

eVertex eVertices[3000];
uint eVertices_count = 0;

void primitive_E_vertex(float x, float y, float z)
{
	eVertices[eVertices_count] = eVertex(x, y, z); // construct and assign 
	eVertices_count ++;
}
I am very satisfied with your responses! Thank you!
You guys forgot to tell me to call the constructor when initializing a variable with the struct. It's okay because I figured.
Actually you might want to add a default constructor so you don't have to supply the parameters if you don't want to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct eVertex
{
	float X, Y, Z;

	// add a default constructor to make initialising optional
	eVertex(): X(0.0f),Y(0.0f),Z(0.0f){}

	// add a constructor to make initialising easier
	eVertex(float x, float y, float z): X(x),Y(y),Z(z){}
};

// now you can do

eVertex e1; // no parameters

eVertex e2(2.4f, 5.9f, 0.7f); // parameters
Last edited on
Oh that just saved me. Thanks! I was having problems with the array :(
Topic archived. No new replies allowed.