Problem init Array!

Started by
1 comment, last by POLSKASH 18 years, 10 months ago
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?
There are some things so stupid that only an intellect could believe them.
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 OKsVertex 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!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Much thanks. Sometimes when you're learning the DirectX API, you lose track of the basics. :/
There are some things so stupid that only an intellect could believe them.

This topic is closed to new replies.

Advertisement