How to instialize an array of classes( in progress )

The Partridge Family were neither partridges nor a family. Discuss.
Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: How to instialize an array of classes

Post by Clodi » January 19th, 2013, 12:36 am

Wow!! I didn't know you can have more than 1 constructor!!
..I am doing exactly what you are telling me to do but my code reports a "fatal external error" of some kind.

This is my class in "Player.h"

Code: Select all

class Particle
{
public:
	Particle( D3DGraphics *gfx, KeyboardClient *kbd );
	Particle();
	~Particle();
	void drawSprite();
	void updatePosition();
private:
	D3DGraphics *pGfx;
	KeyboardClient *pKbd;
	float pX;
	float pY;
	int size;
	int health;
};
And this is the constructor of class game in "game.cpp":

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 );
}
it won't compile. I attach here my code if anyone wants to see
Attachments
yo - Copia.rar
(42.92 KiB) Downloaded 159 times

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:43 am

Ah that's because the default constructor also needs a definition. If you don't want it to do anything you can just add the curly braces in the .h file.

Code: Select all

Particle(){}
And yeah you can overload constructors just like you can overload functions.
Musi

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

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

Re: How to instialize an array of classes

Post by Clodi » January 19th, 2013, 12:50 am

:)

something has been solved!!
it now runs.. and crash saying:
"Unhandled exception at 0x00aa1c51 in Chili DirectX Framework.exe: 0xC0000005: Access violation reading location 0x0000003c."
is it me messing something up with something else? before the "array" idea, my particle Charlie was
chilling around the screen so cheerfully :(

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

Re: How to instialize an array of classes

Post by Musi » January 19th, 2013, 1:00 am

It seems in your ComposeFrame(); function you try to iterate through 20 elements, but there are only 10 in the array. :)
Musi

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

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

Re: How to instialize an array of classes

Post by indus » January 19th, 2013, 8:17 am

Again nice, clean and very readable coding style from you :)
One piece of advice. Once you access your class you dont need to use the scope resolution operator before every class element.

Code: Select all

void Particle::updatePosition()       // on this line you tell the compiler that you operate in class Particle
{
	Particle::pX += Particle::vX;   // here you dont need Particle:: anymore.
	pY += vY;                               // the same here and in every other method
	
}
So you always use it between the method return type and the method name, so that the compiler knows where to look for it. And then in the body of the method you dont need to use it anymore.

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

Re: How to instialize an array of classes

Post by Clodi » January 19th, 2013, 11:39 am

thank you so much guys. :) :)
..it's sad to say but.. I learn more from you then in uni (not even kidding)

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

Re: How to instialize an array of classes

Post by Clodi » January 19th, 2013, 2:15 pm

yes, and now I will have to find a trick to show my particles. Apparently, (tell me if I am wrong) if you design a constructor for a specific class and then you make a single instance of that class, nothing wrong happens, but with arrays the music changes:

intialiasization is a problem and constructors are a problem also. Now, Creating successfully my array "Charlie" of particles doesn't mean that I am able to "see" these particles. In fact when I run the program only one particle shows up. Now, playing around with code and debugger I figured out that all particles show up but they have same "position" and "velocity" therefore they are all superimposed.
What I am now trying to do is to make "position" and "velocity" part of my array, and not part of the class. In this way I should have no problem since the "position" and "velocity" behavior of this array will be effectively specific to that array and not part of the class "particle"

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

Re: How to instialize an array of classes

Post by Musi » January 19th, 2013, 2:26 pm

Remove "srand( (unsigned int) time ( NULL ) );" from your particle class and put it inside your Game constructor instead.

Code: Select all

Game::Game( HWND hWnd,const KeyboardServer& kServer,const MouseServer& mServer )
:	gfx( hWnd ),
	audio( hWnd ),
	kbd( kServer ),
	mouse( mServer )
{
	srand( time ( NULL ) );
	 for(int i = 0; i < 10; i++)
          Charlie[ i ] = Particle( &gfx, &kbd );
}
I think it only needs to be seeded once at the start of the game, instead of for every class. It seems seeding it in every class causes them to all have the same values.
Last edited by Musi on January 19th, 2013, 2:53 pm, edited 1 time in total.
Musi

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

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

Re: How to instialize an array of classes

Post by indus » January 19th, 2013, 2:51 pm

May be even pass pX, pY, vX and vY as parameters for the constructor and do the whole random stuff in the Game constructor.

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

Re: How to instialize an array of classes

Post by Clodi » January 19th, 2013, 3:01 pm

@ Musi
yes, I think it's because of the way I seed it. (with "time()" using NULL )
they get all generated at the same time therefore my random function generates 1 set of random values for my parameters and the same set for each element of the array,
when you go about it using the debugger, you take steps at different points in time and that "cover" the problem,

glad that with your help I finally got it to work :)

@ indus

Yes, but I would probably get the same result. Don' ask me, i don't get classes! but I think that if you put "srand( (unsigned int) time ( NULL ) );" in your class, as the array is generated, every element will get the same seed since they are all generated at the same time.
Doing like you said would be the coolest way, for how I see it, because it would mean that every time I initialise that particular class in my game, my class is not retarded and know perfectly what to do from the start. Unfortunately there might /might not be a way to do that, but I don't know any.
..for now, it makes sense to put "srand( (unsigned int) time ( NULL ) );" in game so that "time" is called every frame.. or at least I am pretty sure it works that way.. :D

Post Reply