How to instialize an array of classes( in progress )
How to instialize an array of classes( in progress )
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.
Re: How to instialize an array of classes
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.
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
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.
Re: How to instialize an array of classes
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.
There are 10 types of people that understand binary.
Those that do, and those that don't.
Re: How to instialize an array of classes
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
Heres my whole project.
- Attachments
-
- Income wars - Copy - Copy.rar
- (641.37 KiB) Downloaded 201 times
Computer too slow? Consider running a VM on your toaster.
Re: How to instialize an array of classes
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
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.
Fyi project came with 2 default constructos in spawn there should only be 1.
Computer too slow? Consider running a VM on your toaster.
Re: How to instialize an array of classes
spawn class
marine class
and than your declaration and initialisation
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(){}
Code: Select all
class Spawn
{
public:
Spawn( Unit* units )
:
units( units )
{}
};
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;
}
};
Code: Select all
Spawn marine[100];
marine[ index ] = new Marine( std::wstring( L"Marine\\marine" ),31,10 );
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
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.
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.