How to instialize an array of classes( in progress )

The Partridge Family were neither partridges nor a family. Discuss.
indus
Posts: 35
Joined: November 7th, 2012, 12:35 am

Re: How to instialize an array of classes

Post by indus » January 17th, 2013, 1:02 am

You seem to be right. And it also seems to work now. Just takes forever to load the sprites.

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: How to instialize an array of classes

Post by cameron » January 17th, 2013, 1:15 am

Its still not working the problem, I think is its calling the default constructor. I really need it to cal the right one.
Computer too slow? Consider running a VM on your toaster.

User avatar
GreatJake
Posts: 169
Joined: January 6th, 2013, 5:13 pm
Location: USA

Re: How to instialize an array of classes

Post by GreatJake » January 17th, 2013, 1:20 am

I added curly braces after your default constructor and everything compiled, but the screen was blank.

indus
Posts: 35
Joined: November 7th, 2012, 12:35 am

Re: How to instialize an array of classes

Post by indus » January 17th, 2013, 1:40 am

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.

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: How to instialize an array of classes

Post by cameron » January 17th, 2013, 1:54 am

That might just work ill try it in a few hours.
Computer too slow? Consider running a VM on your toaster.

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: How to instialize an array of classes

Post by cameron » January 17th, 2013, 4:12 am

I am having difficulties ill try it tomorrow.
Computer too slow? Consider running a VM on your toaster.

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: How to instialize an array of classes

Post by Clodi » January 18th, 2013, 9:19 pm

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? :(

indus
Posts: 35
Joined: November 7th, 2012, 12:35 am

Re: How to instialize an array of classes

Post by indus » January 18th, 2013, 11:03 pm

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 );
}

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: How to instialize an array of classes

Post by Clodi » January 18th, 2013, 11:57 pm

no, mate :( it won't work.

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

Musi
Posts: 106
Joined: November 25th, 2012, 1:06 am

Re: How to instialize an array of classes

Post by Musi » January 19th, 2013, 12:22 am

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 );
}
Musi

There are 10 types of people that understand binary.
Those that do, and those that don't.

Post Reply