Advertisement

arrays of objects ?

Started by April 14, 2002 11:26 AM
8 comments, last by sarkath 22 years, 8 months ago
How do i make arrays of objects in c++ ? I tried: cout << "How many balls are there: "; cin >> number; balls ball[number]; but i get verbally assaulted by the compiler. it seems like I have to tell it how big the array would be in the code like: ball[10]; or it wont work. How do i make a user decided amount of balls in this case ?
jo|

the problem is, that you aren't allowed to creat an array
of a variable length:

int iLength=4;
ball balls[iLength]; // will cause an error.

ball balls[4]; // compiles and works fine

To get a variable length you can create a 'linked-list'. then
you can easily add, remove, sort the data.
or:

int iLength=4;
ball *balls =new(ball[iLength]); // will create an array of variable length

but a linked-list is the better way i think.
--cya felix


[edited by - _BladeRunner_ on April 14, 2002 12:45:39 PM]
Advertisement
You can try using pointers:

int number=0;
int *numballs;

cout << "How many balls?: ";
cin >> number;

numballs = new int[number];

balls ball[numballs];

or something like that.

If thispost = 0 Then
GoBack()

Else
Read()
End If
I'll have a link to the TriFaze website as soon as possible. It's still currently being designed, by myself of course! =) I'll update the URL and my signature as soon as possible.Feel free to send me a message on Yahoo! or AOL IM™. =)
You will have to allocate the memory dynamically (ie at run-time). Look into STD::Vector (I think that's right - I don't use C++ much though).

Or find out about malloc and other memory control functions, then you will know how it works internally.

EDIT :: They (them down there below this post) are right - I'm wrong. Use New/Delete, not malloc etc.

John B

[edited by - JohnBSmall on April 14, 2002 1:20:02 PM]
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
Look up new and delete. You have to dynamically allocate the memory. This isn't something you need to do only for arrays of objects, but for arrays of basic types as well. Alternatively, use a std::vector from the STL. That's what they're there for.

[edited by - SilentCoder on April 14, 2002 1:12:59 PM]
OK, here are your options:

- Dynamically allocate your array (I recommended this):

  int num;cout << "How many balls are there?\t";cin >> num;Ball * ballPtr = new Ball[num];//Do stuff...delete [] ballPtr; //Don''t forget this!  



- Use a linked list (Do this later on)

- Use an STL container like a vector or list. These are reliable, and simple to use. However, I believe (and I''m sure some will disagree with me here) that you should really learn to perform elementary memory allocation, and should have experience with your own arrays and linked lists. Once you have experience, then use the STL for simplicity and reliability.
Advertisement
You''ll need to create a dynamic array using pointers.
Like this.
balls* ball;

cout << "How many balls are there?";
cin >> number;
ball = new balls[number];

-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GED d- s:+ a- C++$ UL++ P++ L++ E W+ N+ o K? w !O M--(+) V-- PS+
PE Y+ PGP t-- 5++ X+ R(+) rv+(++) b+++ DI+ D(+) G e+>++ h r--> y+
----- END GEEK CODE BLOCK-----
geekcode.com
The easiest and best is the following:
#include <vector>using namespace std;int main(){  int number;  cout << "How any balls? ";  cin >> number;  vector< ball > balls;  balls.resize( number );  return 0;} 


[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! | Asking Smart Questions | Internet Acronyms ]
Thanks to Kylotan for the idea!
thanks for all the input mates, much appreciated !!!!
Hello, I just wanted to post a small reply here.

_BladeRunner_: I don''t think that a linked list would be better in this respect, because it eats memory like popcorn (a dynamically created array of bytes just needs 1 byte per item, whereas a linked list storing bytes uses at least 5 bytes per item...1 byte for the byte *gg*, and 4 bytes for the pointer to the next list entry).
Furthermore, running through linked lists can decrease performance enourmously. I''m using linked lists as well, but they are not always the ideal solution. Of course, it really depends on what you expect it to be, it''s the old question of priority: speed vs. size.
Sorting a linked list can be really nasty. If you can spare a few bytes of extra memory I would use a BSP where possible instead. Searching/sorting is extremely fast, and that''s why this technique is used in most 3d-games like UnrealTournament or Quake. Just think it over, dude! *gg*

Yours,

Indeterminatus

--si tacuisses, philosophus mansisses--
Indeterminatus--si tacuisses, philosophus mansisses--

This topic is closed to new replies.

Advertisement