Page 2 of 5

Re: How to instialize an array of classes

Posted: January 17th, 2013, 1:02 am
by indus
You seem to be right. And it also seems to work now. Just takes forever to load the sprites.

Re: How to instialize an array of classes

Posted: January 17th, 2013, 1:15 am
by cameron
Its still not working the problem, I think is its calling the default constructor. I really need it to cal the right one.

Re: How to instialize an array of classes

Posted: January 17th, 2013, 1:20 am
by GreatJake
I added curly braces after your default constructor and everything compiled, but the screen was blank.

Re: How to instialize an array of classes

Posted: January 17th, 2013, 1:40 am
by indus
It is blank because it takes forever to load all the sprites. He creates new surfacesequence for each element of the array so it take really long time to do that. Instead create one instance and use it by all the marines.

Re: How to instialize an array of classes

Posted: January 17th, 2013, 1:54 am
by cameron
That might just work ill try it in a few hours.

Re: How to instialize an array of classes

Posted: January 17th, 2013, 4:12 am
by cameron
I am having difficulties ill try it tomorrow.

Re: How to instialize an array of classes

Posted: January 18th, 2013, 9:19 pm
by Clodi
same problem here: I can't initialise an array of classes.

What I did was:
1) create a new class (named "Particle" with "Particle.h" and "Particle.cpp" to define it)
2) create an instance of that class in "game.h" ("Particle Charlie;")
3) using that in "game.cpp" and EVERYTHING WORKS PERFECTLY.

What I am doing now is:
1) of course create the class, same as before
2) create an array of 10 instances of that class in "game.h" ("Particle[ 10 ] Charlie";)
3) initialising that in "game.cpp" as before but I do it the wrong way apparently.

This is my class game:

Code: Select all

class Game
{
public:
	Game( HWND hWnd,const KeyboardServer& kServer,const MouseServer& mServer );
	void Go();
private:
	void ComposeFrame();

	D3DGraphics gfx;
	KeyboardClient kbd;
	MouseClient mouse;
	DSound audio;
	Particle Charlie[ 10 ];
};
and this is the constructor in "game.cpp":

Code: Select all

Game::Game( HWND hWnd,const KeyboardServer& kServer,const MouseServer& mServer )
:	gfx( hWnd ),
	audio( hWnd ),
	kbd( kServer ),
	mouse( mServer ),
	Charlie[ 10 ]( &gfx,&kbd ) //this is the problem :(
{

}
I repeat, it works perfectly as long as it's one particle but not now with an array. Any help? :(

Re: How to instialize an array of classes

Posted: January 18th, 2013, 11:03 pm
by indus
You will have to initialize every single instance of the class separately. Use for loop for that.

Code: Select all

Game::Game( HWND hWnd,const KeyboardServer& kServer,const MouseServer& mServer )
:   gfx( hWnd ),
   audio( hWnd ),
   kbd( kServer ),
   mouse( mServer )
{
   for(int i = 0; i < 10; i++)
       Charlie[ i] = new Particle ( &gfx, &kbd );
}

Re: How to instialize an array of classes

Posted: January 18th, 2013, 11:57 pm
by Clodi
no, mate :( it won't work.

I guess the constructor must have something before the curly brackets.. no idea why though..

Re: How to instialize an array of classes

Posted: January 19th, 2013, 12:22 am
by Musi
I might be wrong but wouldn't using 'new' create an object on the heap? and wouldn't that require the array to be an array of pointers?

Anyway, if you're creating an array of classes i think the class needs a default constructor because every class calls its constructor when its created. But since there's no real way to pass things to every element of an array on creation (there is but it needs a line of code for every element), it calls the default constructor instead.

Then like indus said, you can iterate through it in a loop, then you can use the assignment operator and call the classes constructor yourself.

Code: Select all

class Paricle
{
Public:
     Particle();    // Default constructor
     Particle( D3DGraphics* pGfx, KeyboardCliet* kbd );    // Constructor you call.
};

Code: Select all

Game::Game( HWND hWnd,const KeyboardServer& kServer,const MouseServer& mServer )
:    gfx( hWnd ),
     audio( hWnd ),
     kbd( kServer ),
     mouse( mServer )
{
     for(int i = 0; i < 10; i++)
          Charlie[ i ] = Particle( &gfx, &kbd );
}