How to instialize an array of classes( in progress )

The Partridge Family were neither partridges nor a family. Discuss.
cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

How to instialize an array of classes( in progress )

Post by cameron » January 15th, 2013, 11:10 pm

I can not figure out how to instiliaze an array of classes. Does anyone know how?
Last edited by cameron on January 20th, 2013, 10:04 pm, edited 1 time in total.
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 15th, 2013, 11:45 pm

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.

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

Re: How to instialize an array of classes

Post by cameron » January 16th, 2013, 12:16 am

I get a problem just making an array of instance of a class. I dont know why.
Computer too slow? Consider running a VM on your toaster.

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

Re: How to instialize an array of classes

Post by Musi » January 16th, 2013, 7:35 am

Are you trying to make it on the heap?
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 16th, 2013, 9:31 am

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

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

Re: How to instialize an array of classes

Post by cameron » January 16th, 2013, 10:58 pm

Heres my whole project.
Attachments
Income wars - Copy - Copy.rar
(641.37 KiB) Downloaded 193 times
Computer too slow? Consider running a VM on your toaster.

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

Re: How to instialize an array of classes

Post by indus » January 16th, 2013, 11:49 pm

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.

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

Re: How to instialize an array of classes

Post by cameron » January 16th, 2013, 11:58 pm

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.
Computer too slow? Consider running a VM on your toaster.

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

Re: How to instialize an array of classes

Post by indus » January 17th, 2013, 12:11 am

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(){}

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, 12:23 am

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.
Computer too slow? Consider running a VM on your toaster.

Post Reply