Archived

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

werdy666

newbie class question

Recommended Posts

hi i am trying to create an unknown number of objects of my class. It seems to want a constant but i want it to be dynamic. I am very much a beginner in classes and do not completely understand them yet. here is my code.. //the actually class definition. class enemy { public: int sprite; bool alive; COORD badpos; }; // to create the enemies i am using this. int x = 0; int y = 0; int num_bad = level * 10; enemy bad[num_bad]; for (int h = 0; h < num_bad; h++) { x = rand()%60+10; y = rand()%3; bad[h].alive = true; bad[h].badpos.X = x; bad[h].badpos.Y = y; bad[h].sprite = 3; } when i compile i get 3 errors relating to the line: enemy bad[num_bad]; error C2057: expected constant expression error C2466: cannot allocate an array of constant size 0 C2133: 'bad' : unknown size what am i doing wrong? Any hints or tips on this would be greatly appreciatted! And what about constructors and destructors? will my class cause memory leaks or anything without them? werdy666 [edited by - werdy666 on April 29, 2002 9:39:36 AM]

Share this post


Link to post
Share on other sites
The compiler needs to know at compile-time how big the array will be, so that it can set aside space for it. If you provide it with a non-const size, then there are no compile-time guarantees about the size of the array. To get around this, you can use a dynamically allocated array like so:

enemy* bad = new enemy[num_bad];

However, you should *always* prefer std::vector to arrays, unless you have a proven need to use arrays. Silly example:


    
#include <vector>

class enemy
{
};

int main()
{
std::vector<enemy> bad;
bad.push_back(enemy());
bad.push_back(enemy());

enemy e = bad[0];

bad.resize(20);
bad[19] = enemy();
}


A vector will expand to accommodate the number of items you add to it. Read more about it in your favourite C++ library reference.


[C++ FAQ Lite | ACCU | Boost | Python]

[edited by - SabreMan on April 29, 2002 9:53:43 AM]

Share this post


Link to post
Share on other sites
You have to use a constant size to declare an array. You cannot use a variable to declare the size of the array.You used this:
int num_bad = level * 10;
enemy bad[num_bad];
That will not work because num_bad is not a constant. So try this instead:
const int num_bad = level * 10;
enemy bad[num_bad];
That should work.

quote:

And what about constructors and destructors? will my class cause memory leaks or anything without them?


No. If you don''t have one, the compiler should automatically generate one for you.


------------------------------
BASIC programmers don''t die, they just GOSUB and don''t return.

Share this post


Link to post
Share on other sites
Actually, it''s an array problem, not a class problem. When you create an array
of objects, you have to specify its size with a constant, not a variable.

enemy bad[100]; //this will work

const int num_bad = 100; // this will also work
enemy bad[num_bad];

int num_bad = othervariable * 10; // this is not a constant, and
enemy bad[num_bad]; // so you get an error

the problem is that you are using static arrays, and you need to use dynamic arrays.
You should do it like this:

enemy* bad;
bad = new enemy[num_bad];

There you go. I hope you know about pointers and dynamic memory allocation,
it really helps. If you want to resize your array, try this:

delete [] bad;
bad = new enemy[num_bad];

To resize your array, just delete it and make a new one. And remember to delete your
array once you''re done with it, or else you''ll get errors. Any questions?

- Bong, James Bong

Share this post


Link to post
Share on other sites
Yes, thats all fine but as Sabreman says vectors are almost always preferable to arrays. They save you a lot of memory management headaches.

-

Geocyte Has Committed Suicide.

Share this post


Link to post
Share on other sites
thanks for all your quick replies!

yes it was an array problem and not class.

i did try the dynamic array method and it worked, however probably cause i am a newbie i was getting errors during runtime. No idea why!

So I tried using vector, i just copied and pasted that code of sabreman''s straight into my program and it works like a charm straight away!

Now i just gotta work out what all that code actually means lol

Anyone know of any good descriptions or tutorials on vectors? And Vectors are part of the STL right?

And would a linked list also work instead of a class? I have no idea what linked lists are really. Should I just stick with classes and vector until i get the hang of it, then go and learn linked lists?

Again, thanks all for your replies! Very much appreciatted!

Werdy666

Share this post


Link to post
Share on other sites
http://www.sgi.com/tech/stl/
http://directory.google.com/Top/Computers/Programming/Languages/C++/Class_Libraries/STL/Tutorials/

(edit) "Essential C++" by Stanley Lippman.
-

Geocyte Has Committed Suicide.

[edited by - geocyte on May 1, 2002 8:02:54 PM]

Share this post


Link to post
Share on other sites