Jump to content
  • Advertisement
Sign in to follow this  
richie2000

Creating multiple objects - help

This topic is 3986 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'm still new to C++ and trying to create multiple objects. Say I have a basic monster class: class Monster { int m_MaxTimeUnits; int m_CurrentHealth; }; I can use: Monster a; Monster b; Monster c; to create the objects, but it's not very user friendly. Is it possible to do create multiple objects using a 'for' loop (or similar) where you parse the number of monsters to be created? void CreateMonsters(int num_monters) { for(int x=0; x < num_monsters; x++) { // create 5 monsters if CreateMonsters(5) was called. // } } If not, how is the best way to solve this? Thanks

Share this post


Link to post
Share on other sites
Advertisement
I think what you are trying to do is create an array of Monster objects?

Try this (this assumes you're creating your Monsters dynamically and not as value objects, though):



Monster** mArray;
void createMonsters (int num_monsters)
{
for(int i = 0; i <= num_monsters; i++)
{
mArray = new Monster();
}
}



Untested. I'm probably wrong but I don't work with arrays, I use the STL.

Share this post


Link to post
Share on other sites
That will not work because array in "Monster** mArray" is never created. When you do this: "mArray = new Monster();", you will be addressing unknown memory. Here is what I'd do; notice the slight differences:


Monster* mArray;
void createMonsters (int num_monsters)
{
mArray = new Monster[num_monsters];
for(int i = 0; i <= num_monsters; i++)
{
mArray = Do whatever you need to do to initialize each individual monster;
}
}

//and then, later on, you are going to have to call this before you exit:
delete [] mArray;

Share this post


Link to post
Share on other sites
Quote:
Original post by ukdeveloper
Untested. I'm probably wrong but I don't work with arrays, I use the STL.

Then why don't you educate the topic starter about the use of STL container classes? :)


@richie2000: while arrays and dynamic arrays could be used to solve your problem, C++' standard library provides a few container classes that allow you to store objects in them. You can add new ones or remove existing ones whenever you want, without the hassle of manually managing the memory or limiting yourself to a certain number of objects at compile-time. Read up on std::vector and std::deque to get an idea of the possibilities.

Of course, because C++ is such a complex language, you'll need to keep a few things in mind. Objects may be copied around when a vector resizes itself, for example, so you'll probably want to write your own copy constructor for your Monster class. Or use a std::vector that contains pointer-to-Monsters rather than Monster objects directly. Though in that case, a smart pointer is probably smarter (heh) because creating objects on the heap (with the 'new' keyword) requires you to manually clean them up (using 'delete'). That probably sounds pretty complex, and yeah, it is, mostly because C++ is an old language with a background in C, so some solutions involve a bit of patchwork.


Lets just say I'm happy that higher-level languages have since been invented. ;)

Share this post


Link to post
Share on other sites
Thanks everyone for replying.

Captain P:
==========
I read up on STL containers and figured that vectors were the way to go.
But, they always failed when I put them in a loop to create multiple objects.

Eventually I got frustrated and tried arrays because they're easier to understand.

Haven't researched smart pointers yet, that'll be next.


ender7771:
==========
My complier (g++) didn't like this line:
mArray = new Monster[num_monsters];

expected type-specifier before ‘Monster’
error: cannot convert ‘int*’ to ‘Monster*’ in assignment
error: expected `;' before ‘Monster’


ukdeveloper:
============
ender7771 was right, it says the array wasn't created.


I was hoping there would be an industry standard way of doing this.
Richie

Share this post


Link to post
Share on other sites
Quote:
Original post by richie2000
Thanks everyone for replying.

Captain P:
==========
I read up on STL containers and figured that vectors were the way to go.
But, they always failed when I put them in a loop to create multiple objects.

Eventually I got frustrated and tried arrays because they're easier to understand.


Post your code.

Quote:

I was hoping there would be an industry standard way of doing this.
Richie


That would be to use the Standard C++ Library (of which a subset is often referred to as the STL).

Share this post


Link to post
Share on other sites
Using the basic class in my first post, what error

vector<Monster *> MonsterTeam;
vector<Monster *>::iterator p;

// adding pointers to the monster class into a vector //
for(int x = 0; x < 4; x++)
{
MonsterTeam.push_back(new Monster());
}

// iterate through the vector using WHILE loop ** FAILS **//
p = MonsterTeam.begin();
while(p != MonsterTeam.end())
{
cout << MonsterTeam[*p].m_CurrentHealth;
p++;
}


// iterate through the vector using FOR loop ** FAILS **//
for(p = MonsterTeam.begin(); p < MonsterTeam.end(); p++)
{
cout << MonsterTeam[*p].m_CurrentHealth;
}


It fails on the "cout" lines, so I guess it isn't accessing the vector correctly.
I think I'm on the right track here, but I can't figure out what's wrong.

Share this post


Link to post
Share on other sites
If you were trying to access vectors like arrays without actually putting objects in them, then your program would have crashed. Is that what happened?

You can't do this:

//create 5 monsters
for(int i = 0; i < 5; i++)
myVector = Monster();


You have to allocate space in the vector first:

//create 5 monsters without crashing
for(int i = 0; i < 5; i++)
myVector.push_back(Monster());

Share this post


Link to post
Share on other sites
I think I've got it now.

My "cout" lines should have been:

cout << (*p)->m_CurrentHealth;

p is an iterator that was initialized to point to vector<MonsterTeam>
It works now.

Thanks everyone!!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!