How to instialize an array of classes( in progress )
Re: How to instialize an array of classes
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
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.
Re: How to instialize an array of classes
I added curly braces after your default constructor and everything compiled, but the screen was blank.
Re: How to instialize an array of classes
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
That might just work ill try it in a few hours.
Computer too slow? Consider running a VM on your toaster.
Re: How to instialize an array of classes
I am having difficulties ill try it tomorrow.
Computer too slow? Consider running a VM on your toaster.
Re: How to instialize an array of classes
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:
and this is the constructor in "game.cpp":
I repeat, it works perfectly as long as it's one particle but not now with an array. Any help?
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 ];
};
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 :(
{
}
Re: How to instialize an array of classes
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
no, mate it won't work.
I guess the constructor must have something before the curly brackets.. no idea why though..
I guess the constructor must have something before the curly brackets.. no idea why though..
Re: How to instialize an array of classes
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.
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.
There are 10 types of people that understand binary.
Those that do, and those that don't.