Jump to content
  • Advertisement
Sign in to follow this  
f001error

Shorthand for initalising an array of structs

This topic is 4443 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 ?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It's probably better to avoid the old C-style struct initialization and use a constructor instead. Try that.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!