Sign in to follow this  

How do you declare an array of classes?

This topic is 3864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I declare the class like so Character Hero(graphics, collision, 100, 100); I want to make an array of Character class objects... I tried... Character Hero[10](graphics, collision, 100, 100); But that does not work. Any suggestion would be greatly appreciated. Regards Chad

Share this post


Link to post
Share on other sites
Short answer:

Character Hero[10];

This will initialize an array Hero with 10 elements. Each element will be of type Character. So when the array Hero is created, it initializes each element to the default constructor of the type Character. So if you havn't declared a default constructor for your class Character, then all the data members will be set to the default, or 0. And if you wan't to initialize each element in the array to the same thing, you would write

Character Hero[10] = { graphics, collision, 100, 100 };

(Assuming that graphics, collision, 100, 100 are all valid)

Share this post


Link to post
Share on other sites
try


Character Hero[]={
Hero(graphics, collision, 100, 100),
Hero(graphics, collision, 100, 100),
Hero(graphics, collision, 100, 100),
Hero(graphics, collision, 100, 100),
Hero(graphics, collision, 100, 100),
Hero(graphics, collision, 100, 100),
Hero(graphics, collision, 100, 100),
Hero(graphics, collision, 100, 100)
};

//though a array of pointers would be better
//not sure i got this right though,

Character **Hero;

void init(){
Hero = new *Hero[10]

for (int i=0;i<10;i++){
Hero[i]= new Hero(graphics, collision, 100, 100);
}
}


Share this post


Link to post
Share on other sites
Unfortunately, there's no concise syntax in C++ for doing what you are trying to do with a class that requires arguments passed in the constructor.

You'd have to do it either like this (using pointers and dynamic memory allocation):

Character *Heroes[10];
for ( int i = 0; i < 10; ++i )
{
Heroes[i] = new Character(graphics, collision, 100, 100);
}

//remember that when you're done with those objects you now have
//to release the memory
for ( int i = 0; i < 10; ++i )
{
delete Heroes[i];
}




or you could use an STL container:

Character *generateHero()
{
//graphics & collision need to be accessible at this scope
return new Character(graphics, collision, 100, 100);
}

std::vector<Character*> Heroes;
std::generate_n( Heroes.begin(), 10, generateHero );



I'm guessing that both methods utilize parts of C++ that you haven't learned yet: dynamic memory allocation on one hand, and also the STL on the other.

If Character is small, and has a default constructor (a constructor that takes zero arguments) and you have a properly overloaded operator= then I suppose you could also do:


Character Hero[10];
Character heroTemplate(graphics, collision, 100, 100);
for ( int i = 0; i < 10; ++i )
{
Hero[i] = heroTemplate;
}



However, in general, you don't often make arrays of large classes without referencing them in the array by pointer.

-me

Share this post


Link to post
Share on other sites
I wish I could use STL but I am doing this cross platform (i.e. in windows and Linux). I don't want to take the chance of things messing up. I will try the examples above and let everyone know the results

Thanks

Chad

Share this post


Link to post
Share on other sites
STL is part of the c++ standard and is fully cross-platform, only the windows api header files will cause problems

Share this post


Link to post
Share on other sites
Quote:

I wish I could use STL but I am doing this cross platform (i.e. in windows and Linux).


This makes me laugh. The SC++L (what you really mean when you say STL) is standard. It is part of the language; it is just as cross-platform as the rest of the language... unless you misuse it (but that applies equally to everything in C++), you'll get the behavior it guarantees you.

So use the SC++L. Don't make things harder for yourself based on speculative misinformation.

Quote:

I don't want to take the chance of things messing up.

Technically, you probably have a better chance of "things messing up" if you use standard C++ data types (char, short, long, int, et cetera) than the SC++L, because the standard defines so very little about those types in practice (only relative sizes, and that their size is expressed in multiples of sizeof(char), which is guaranteed to be 1, but that doesn't mean its one byte, et cetera).

Share this post


Link to post
Share on other sites
Quote:
Original post by Shakedown

Character Hero[10] = { graphics, collision, 100, 100 };



This is not valid C++ syntax. The correct way to initialize a static array of objects using a list is:


Character Hero[10] = { Character(graphics, collision, 100, 100),
Character(graphics, collision, 100, 100),
Character(graphics, collision, 100, 100),
Character(graphics, collision, 100, 100),
Character(graphics, collision, 100, 100),
Character(graphics, collision, 100, 100),
Character(graphics, collision, 100, 100),
Character(graphics, collision, 100, 100),
Character(graphics, collision, 100, 100),
Character(graphics, collision, 100, 100) };


Using an array (or an STL container) of pointers would be a better approach, though.

Share this post


Link to post
Share on other sites
Quote:
Original post by Palidine
or you could use an STL container:
*** Source Snippet Removed ***


std::vector has a constructor for the case where each initial element is the same; there's no need to invoke the subtler stuff from <algorithm>. We don't even need to store things by pointer:


std::vector<Character> Heroes(10, Character(graphics, collision, 100, 100));


In general, adding pointers to your design because "it's hard to do it without pointers" is the wrong design decision: it will make lots of other things harder (at least if you care about doing them properly), and you'll be worse off.


Share this post


Link to post
Share on other sites
I am having no problems declaring the vector like stated above. When I go to use it though I am getting errors. For example something like...

Heros[2].MoveRight();

is giving me

game.obj : error LNK2019: unresolved external symbol __imp___CrtDbgReportW referenced in function "public: class Character & __thiscall std::vector<class Character,class std::allocator<class Character> >::operator[](unsigned int)" (??A?$vector@VCharacter@@V?$allocator@VCharacter@@@std@@@std@@QAEAAVCharacter@@I@Z)
: fatal error LNK1120: 1 unresolved externals

Share this post


Link to post
Share on other sites

This topic is 3864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this