• Advertisement
Sign in to follow this  

Problem init Array!

This topic is 4637 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 must be losing it or something, but I'm having problems initializing an array of structures. Here's the relevant code:
typedef struct
{
	float x, y, z;
	float u, v;
}sVertex;

struct Particle
{
    D3DXVECTOR3 Position;         // Position of particle in world-space
    D3DXVECTOR3 OldPos;           // Position in last frame
    D3DXVECTOR3 Velocity;         // Velocity along the three axes
    D3DXVECTOR3 Acceleration;     // Acceleration along the three axes
    int Age;                    // Age of particle (sum of TimePassed passed to UpdateSystem)
    float Size;                 // Size of particle
    bool Alive;                 // Does this particle need to be updated?
	sVertex Verts[4];
};

Particle *Particles;

Particles = new Particle[NumParts];

	for(int i = 0; i < NumParts; i++)
	{
		Particles.Verts = {  {  0.0f, (float)height, 0.0f, 0.0f, 0.0f },
								{ (float)width, (float)height, 0.0f, 1.0f, 0.0f },
								{  0.0f, 0.0f, 0.0f, 0.0f, 1.0f },
								{ (float)width, 0.0f, 0.0f, 1.0f, 1.0f }  };
	}



Generates this compilation error: c:\Documents and Settings\Owner\Desktop\Testing Ground\Testing Ground\ParticleSystem.cpp(9) : error C2059: syntax error : '{' c:\Documents and Settings\Owner\Desktop\Testing Ground\Testing Ground\ParticleSystem.cpp(9) : error C2143: syntax error : missing ';' before '{' c:\Documents and Settings\Owner\Desktop\Testing Ground\Testing Ground\ParticleSystem.cpp(9) : error C2143: syntax error : missing ';' before '}' c:\Documents and Settings\Owner\Desktop\Testing Ground\Testing Ground\ParticleSystem.cpp(9) : error C2143: syntax error : missing ';' before ',' c:\Documents and Settings\Owner\Desktop\Testing Ground\Testing Ground\ParticleSystem.cpp(10) : error C2143: syntax error : missing ';' before '{' And so on for all the lines in the for loop. Can someone tell me what I'm doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
POLSKASH,

The reason you're getting the compile errors is because you're attempting to use what's called struct (or record) initialization on the array of Verts.

as an example, look at this code.


// Given your structure from before, sVertex, the following line OK
sVertex vertex = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };

//This however is NOT OK.
sVertex vertex2;
vertex2 = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };



As you can see from the example above its ok to initialize a struct when the struct is first created, however you cannot initialize it after it has been created. In the sample code you provided the array of sVertex structs "Verts[4]" is being created when you call
Particles = new Particle[NumParts];

Because of that, you cannot use the record initialization form inside of the loop. You must individually set the components of each record. The best way to solve this problem is to give your struct a "set" function. Then as you're iterating through the structs, just pass the values into the function. Here's the code

struct sVertex
{
void Set( float fX, float fY, float fZ, float fU, float fV )
{
x = fX; y = fY; z = fZ;
u = fU; v = fV;
}
float x, y, z;
float u, v;
};

.......

for(int i = 0; i < NumParts; i++)
{
Particles.Verts[0].Set( 0.0f, height, 0.0f, 0.0f, 0.0f );
Particles.Verts[1].Set( width, height, 0.0f, 1.0f, 0.0f );
Particles.Verts[2].Set( 0.0f, 0.0f, 0.0f, 0.0f, 1.0f );
Particles.Verts[3].Set( width, 0.0f, 0.0f, 1.0f, 1.0f );
}



I modified your code a bit to demonstrate the point. As well, many people would tell you that once you've given a struct a function its not really a struct any more. Its more of a class. Its up to you if you want to change your vertex type from a struct to a class.

Good luck!

Share this post


Link to post
Share on other sites
Much thanks. Sometimes when you're learning the DirectX API, you lose track of the basics. :/

Share this post


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

  • Advertisement