Page 1 of 5

How to instialize an array of classes( in progress )

Posted: January 15th, 2013, 11:10 pm
by cameron
I can not figure out how to instiliaze an array of classes. Does anyone know how?

Re: How to instialize an array of classes

Posted: January 15th, 2013, 11:45 pm
by GreatJake
I just had this problem a few minutes ago, what I did was add a default constructor to my class, and keep you old constructor. And you should be able to use a loop to initialize your array.

Edit: is that what you were asking? after re-reading your post i'm not sure if this is what you meant.

Re: How to instialize an array of classes

Posted: January 16th, 2013, 12:16 am
by cameron
I get a problem just making an array of instance of a class. I dont know why.

Re: How to instialize an array of classes

Posted: January 16th, 2013, 7:35 am
by Musi
Are you trying to make it on the heap?

Re: How to instialize an array of classes

Posted: January 16th, 2013, 9:31 am
by indus
It is always better when you put a small code example showing what exactly are you trying to do and what isnt working.

Code: Select all

class A
{
    int age;
    string name; 
    A(int a , string b)  : age(a),  name(b) {}
    ~A();
};

A arrExample [2] = { {15, "John"}, {16, "Travolta"} };
vector <A> vectorExample { {20, "Quentin "}, {16, "Tarantino"} };

Re: How to instialize an array of classes

Posted: January 16th, 2013, 10:58 pm
by cameron
Heres my whole project.

Re: How to instialize an array of classes

Posted: January 16th, 2013, 11:49 pm
by indus
I am not able to compile your project at all.It gives me some linker error that does not say anything helpful. Probably somethings messed up with the project configuration. Anyway as I already wrote, you should just give us a small piece of code. Like the one I posted. Showing the constructor of the class and how you create and initialize the array. And than explain what exactly happens and what does not work.

Re: How to instialize an array of classes

Posted: January 16th, 2013, 11:58 pm
by cameron
I get the same error too and its because of the array. Get rid of the array and the default constructor and instialize the class. It gives no errors. The problem is the class.

Fyi project came with 2 default constructos in spawn there should only be 1.

Re: How to instialize an array of classes

Posted: January 17th, 2013, 12:11 am
by indus
spawn class

Code: Select all

class Spawn
{
public:
	Spawn( Unit* units )
		:
	units( units )
	{}
};
marine class

Code: Select all

class Marine : public Unit
{
public:
	Marine( std::wstring basename, unsigned int nSurfaces, unsigned int nHoldFrames, D3DCOLOR key = D3DCOLOR_XRGB( 255,255,255 ) )
		:
	Unit( basename, nSurfaces, nHoldFrames, key )
	{
		BankCost = 1;
		AttackDamage = .0168;  /*1/fps*/
		Health = 2;
		Movement = 6;
		x = 0;
		y = 250;
	}
};
and than your declaration and initialisation

Code: Select all

Spawn marine[100];
marine[ index ] = new Marine( std::wstring( L"Marine\\marine" ),31,10 );
This just cant work. You declare objects of type Spawn and than when initialising say they are of type Marine. You dont have any relation between Spawn and Marine so you cant do that.
Your Marine class inherits from class Unit. So what you could do if you wanted and needed to is declare
Unit myUnits[100];
and than say
for(...)
myUnits = new Marine(....);

The other way round will not work.


Edit:
In addition to what cameron wrote about the two default constructors. You will also need to put curly brackets after the round ones so that the constructor is declared and defined aswell. Thats the source of the annoying error.
Spawn(){}

Re: How to instialize an array of classes

Posted: January 17th, 2013, 12:23 am
by cameron
I should be able to do this.

for(int index = 0; index < 101; index++ )
{
marine[ index ] = new Marine( std::wstring( L"Marine\\marine" ),31,10 );
}

because the spawn class takes the parameter Unit* units. The marine class inherits the unit class. So it takes a pointer to a unit but I send it a pointer to marine. So if i put virtual void on those functions it should work. It worked like this before with no array.