Shorthand for initalising an array of structs

Started by
13 comments, last by NotAYakk 17 years, 7 months ago
G'day For some crazy reason, c++ isn't liking the shorthand shorthand syntax for array initalisation when my array is an array of structs right. This works fine:

int myArray[4];
myArray[] = {0,1,2,3}
But this doesnt: CustomVertextFormat vertices[4];

vertices[] =
	{
		{ 0.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 0.0f,  0.0f,  }, // top left
		{ width, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 1.0f,  0.0f,  }, // top right
		{ 0.0f, height, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 0.0f,  1.0f,  }, // bottom left
		{ width, height, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 1.0f,  1.0f,  }, // bottom right

	}
I could go vertices[0].x = 0.0f; vertices[0].y = 0.0f ...etc but I'ld prefer to do it the quick and easy way =P I'm guessing its something stupidly simple, any ideas ?
Advertisement
Could it be that 'CustomVertextFormat' was meant to be 'CustomVertexFormat'?

Either way, I'm not sure that you can initialize the struct array in that manner.
http://blog.protonovus.com/
Initial guess is that you have a extra ',' before your } on each line.
int myArray[] = {0,1,2,3};
works fine. The initializer list has to be added right after the declaration, unless I'm mistaken.
So far, it means that
CustomVertexFormat vertices[4] = { ... };
should work, provided that CustomVertexFormat is an aggregate type.
Regards,
Another way is using the constructor of your vertex class:

vertices[] = { vertex(a,b,c), vertex(d,e,f) ... }

That also allows you to skip unused arguments and do other interesting things.
It's probably better to avoid the old C-style struct initialization and use a constructor instead. Try that.
http://www.gamedev.net/community/forums/topic.asp?topic_id=230443

You may find this thread interesting
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
CustomVertextFormat test = { 0.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 0.0f,  0.0f };


Note that the above doesn't compile.

Your CustomVertextFormat verticies are not POD data structures.

Define a full constructor your CustomVertextFormat.

Quote:
Note that the above doesn't compile.

Your CustomVertextFormat verticies are not POD data structures.

Define a full constructor your CustomVertextFormat.


Why arent they POD (plain old data ?) structures ? They are just structs...

Quote:
int myArray[] = {0,1,2,3};

works fine. The initializer list has to be added right after the declaration, unless I'm mistaken.
So far, it means that

CustomVertexFormat vertices[4] = { ... };

should work, provided that CustomVertexFormat is an aggregate type.
Regards,


Umm, yeah your right.

int myArray[4];
myArray[] = {0,1,2,3}

Doesnt actually work, although last night i thought it was lol. Yay for lack of sleepies :P

BTW what is an aggregate type ?





okay, thanks guys looks like im off to make a class full of public members, with no methods, you know, like a struct :P
CustomVertextFormat vertices[4] =	{		{ 0.0f, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 0.0f,  0.0f  }, // top left		{ width, 0.0f, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 1.0f,  0.0f  }, // top right		{ 0.0f, height, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 0.0f,  1.0f  }, // bottom left		{ width, height, 0.0f, 1.0f, D3DCOLOR_XRGB( 255, 255, 255), 1.0f,  1.0f  } // bottom right	};

that doesnt work?

This topic is closed to new replies.

Advertisement