Archived

This topic is now archived and is closed to further replies.

Creating a launcher in C++ (problems)

This topic is 5048 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

Hi. I''ve created a termite simulation in C++ which loads in a window (it features a grid where termites use emergent behaviour to create mounds of woodchips). Now that part is finished, and my supervisor (I''m writing this program so he can demo it in a lab to his AI students) has asked me to add some buttons/sliders so that the number of termites/woodchips or the speed of the sim can be changed without having to compile. Now at the moment my program simply starts by opening up a window and starting the simulation inside it. What I want to do is have a launcher that appears first so you can change the settings and then click SIMULATE! or RUN! or something. The main problem I have is that the only way I could have an array of objects initialize with a (variable) number of items is to specify a DEFINE in the header file, for example : Header File contains : #define MAP_WIDTH 50 #define MAP_HEIGHT 50 #define WOODCHIPS 1000 #define TERMITES 100 CPP file contains : int MAP[MAP_WIDTH * MAP_HEIGHT]; int WOOD[WOODCHIPS * 2]; Termite Them[TERMITES]; All those arrays are global - so that they can be used both by the main code and the code within the Termite objects. Using DEFINE was the only way I could get those arrays to initialise, because otherwise I get an error message like : -----CODE------ int Termite_array_size = 100; Termite Them[Termite_array_size]; ------------------ error C2057: expected constant expression error C2466: cannot allocate an array of constant size 0 So, how would I write this launcher so that these variables (currently DEFINEd in the header file) to be changable? Can you actually specify DEFINITIONS like this like you can do with normal variables? Any help would be great! Cheers, SFA.

Share this post


Link to post
Share on other sites
Search for 'dynamic arrays' on this forum. Basically, dynamically allocating an array involves holding a pointer to it, allocate memory using new and free using delete, like this


Termite* termites;
termites=new Termite[100];

//later on
delete[] termites


Note that new throws std::bad_alloc if the computer is out of memory.

A forum search will give you more information about dynamic allocation that you'd ever need to know

[edit]Or a std::vector [/edit]

[edited by - CloudNine on February 20, 2004 7:21:40 AM]

Share this post


Link to post
Share on other sites