Archived

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

jammyt

How do I do an array of objects

Recommended Posts

jammyt    122
I'm writing a version of the classic Chuckie Egg but can't get my objects to work. I've not programmed in C++ since uni 3 years ago and I can't remember how to do a lot of things. I've tried looking this up on the net but to no avail. I've got a class called Goose. When I create 1 goose it is fine but I want to create an array of geese so I can have multiple baddies. I've got a global variable: Goose *geese[2]; and then in my main code: geese[0] = new Goose(); geese[1] = new Goose(); Does this look correct? I've a bit confused because my application is now crashes strangely and I'm thinking that the memory may not have been allocated correctly or something. As I said if I create 1 goose with the following code it works fine: Goose goose1; goose1 = Goose(); Any ideas to what I'm doing wrong with the array? Jam [edited by - jammyt on August 11, 2003 6:12:51 PM]

Share this post


Link to post
Share on other sites
GrinningGator    122
quote:
Original post by jammyt
I''ve got a global variable:
Goose *geese[2];

and then in my main code:
geese[0] = new Goose();
geese[1] = new Goose();


The code you posted looks fine. The problem is probably somewhere else. Can you post a minimal example that compiles but still has the problem. Often reducing your code down to a minimal example that still shows the problem will show you where the problem is and you''ll end up answering your own question.

Share this post


Link to post
Share on other sites