Jump to content
  • Advertisement
Sign in to follow this  
SiliconMunky

Help with structs, (previously titled, untitled)

This topic is 4722 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

I always seem to get annoyed when using structs, could someone please explain why I can't do the following? struct D3DSKYBOXVERTEX { float fX, fY, fZ, tu, tv; }; D3DSKYBOXVERTEX vertexData1 = { -1.0f, 1.0f, 1.0f, 0.0f, 0.0f }; vertexData1 = { 1.0f, 1.0f, 1.0f, 1.0f, 0.0f }; Is there anyway to set all the properties of a struct without having to do vertexData1.fX = something; vertexData1.fY = something; ? Thanks in advance for the help. [Edited by - SiliconMunky on June 18, 2005 8:40:08 PM]

Share this post


Link to post
Share on other sites
Advertisement
You could make a copy-constructor and an assignment operator, then it'd be as easy as:
vertexData1 = D3DSKYBOXVERTEX( 1.0f, 1.0f, 1.0f, 1.0f, 0.0f );

Share this post


Link to post
Share on other sites
ok i changed my struct to


struct D3DSKYBOXVERTEX
{
float fX, fY, fZ, tu, tv;
D3DSKYBOXVERTEX(float x, float y, float z, float u, float v)
{
fX=x; fY=y; fZ=z; tu=u; tv=v;
}

};


and changed my code to


D3DSKYBOXVERTEX vertexData1 = { -1.0f, 1.0f, 1.0f, 0.0f, 0.0f };
vertexData1 = D3DSKYBOXVERTEX( 1.0f, 1.0f, 1.0f, 1.0f, 0.0f );


which gave me the error


error C2552: 'vertexData1' : non-aggregates cannot be initialized with initializer list


What does that mean, and why did adding a constructor cause this to happen?

I fixed it by changing the code to


D3DSKYBOXVERTEX vertexData1 = D3DSKYBOXVERTEX( -1.0f, 1.0f, 1.0f, 0.0f, 0.0f );
vertexData1 = D3DSKYBOXVERTEX( 1.0f, 1.0f, 1.0f, 1.0f, 0.0f );


I'm curious why you said I need to make an assignment operator?

Share this post


Link to post
Share on other sites
An aggregate is an array or a class with no user-defined constructors, no private or protected non-static data members, no base classes, and no virtual functions. You can only use aggregate types with an initializer clause.

You should probably add a default constructor, you do not need a copy constructor nor assignment operator.


struct D3DSKYBOXVERTEX
{
float fX, fY, fZ, tu, tv;
D3DSKYBOXVERTEX(float x, float y, float z, float u, float v)
:fX(x), fY(y), fZ(z), tu(u), tv(v) { }
};

D3DSKYBOXVERTEX vertexData1 = ( -1.0f, 1.0f, 1.0f, 0.0f, 0.0f );
vertexData1 = D3DSKYBOXVERTEX( 1.0f, 1.0f, 1.0f, 1.0f, 0.0f );


Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
*** Source Snippet Removed ***
I'm sure you meant to remove the " = " on the second to last line:
struct D3DSKYBOXVERTEX
{
float fX, fY, fZ, tu, tv;
D3DSKYBOXVERTEX(float x, float y, float z, float u, float v)
:fX(x), fY(y), fZ(z), tu(u), tv(v) { }
};

D3DSKYBOXVERTEX vertexData1( -1.0f, 1.0f, 1.0f, 0.0f, 0.0f );
vertexData1 = D3DSKYBOXVERTEX( 1.0f, 1.0f, 1.0f, 1.0f, 0.0f );


Share this post


Link to post
Share on other sites
Quote:
Original post by SiliconMunky
I'm curious why you said I need to make an assignment operator?


When you do the assignment (vertexData1 = ) you're using the assignment operator, ofcourse you could do as Washu says and use the default assignment operator. But it's good practice to define both (the copy constructor and the assignment operator) if you're defining one, and you can easily make them use each other and share code. Also I forgot to mention a few details (like making a constructor as Washu mentioned), which is what I intended with my example, just the neurons didn't click.

Share this post


Link to post
Share on other sites
Quote:
Original post by abdulla
Quote:
Original post by SiliconMunky
I'm curious why you said I need to make an assignment operator?


When you do the assignment (vertexData1 = ) you're using the assignment operator, ofcourse you could do as Washu says and use the default assignment operator. But it's good practice to define both (the copy constructor and the assignment operator) if you're defining one, and you can easily make them use each other and share code. Also I forgot to mention a few details (like making a constructor as Washu mentioned), which is what I intended with my example, just the neurons didn't click.


Assignment operators are unnecessary for POD types (which is what we have here). As are copy constructors and deconstructors.

Of course, if you have one, you genereally need all three (of assignment, copy, and deconstructor).

Share this post


Link to post
Share on other sites
Which is why I mentioned the use of the default copy constructor/assignment operator. In the end what I meant was to make a constructor and the assignment operator would do the rest, I just didn't explain myself very well, and then tried to over explain, so my mistake.

Just need to keep in mind that the default is to do a member-wise copy, so if that's not what you intend, either define the needed functions or declare them private so accidents won't happen. Hope all this ranting helped.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!