Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 06-23-2004, 12:07 PM   #1
Member (10 bit)
 
Iniamyen's Avatar
 
Join Date: Aug 2003
Location: Seattle, WA
Posts: 517
Send a message via AIM to Iniamyen
Boom C++ objects having arrays

Another frustrating C++ question for anyone who can spare a minute...

I'm trying to create a class which has an array member (contains an array, is this the correct term?) The class has functions make_array() and show_array. The make_array() function just prompts the user for array indices and the filename to be read, and then goes through and creates the array from the file. I have verified my parsing routines and they all work. The show_array() function simply prints the contents of the array to the console using cout.

The problem is that when I tell the object that it has an array in the private section, I have to specify a size for it. If I don't, and just use object_type array_name[] format, it defaults the array to zero size. I want to be able to have the user prompts in the make_array() function define the size of the array, however to even let the thing know I have an array of finite size, I have to specify an actual size at runtime.

I've tried creating an integer variable and saying object_type array_name(variable), and letting the user prompts define the value of the variable, but the compiler does not like this at all.

Any help appreciated!
__________________
Desktop
| Antec Performance Plus 1080AMG | Antec 430W PSU | Intel D875PBZ | Intel P4 3.0C |
| 4x512 MB dual-channel DDR400 Kingston HyperX | ATI Radeon 9800 pro 256 MB | Creative Sound Blaster Audigy Gamer |
| LiteOn DVD-ROM | LiteOn CDRW | 36 GB WD Raptor | 250 GB WD SATA2 | Sony Floppy | XP pro |
Iniamyen is offline   Reply With Quote
Old 06-23-2004, 12:15 PM   #2
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
You can't dynamically size arrays in C++ (actually, I think there is a way, but it is not pleasant). You have to supply a size at compile time, not later. Obviously, this is a huge pain. You can either set the size at compile time to be larger than any size you think you need (a waste and dangerous if you guess too low), or you can use vectors.
doctorgonzo is offline   Reply With Quote
Old 06-23-2004, 12:15 PM   #3
Member (13 bit)
 
DrZaius's Avatar
 
Join Date: Jul 2000
Location: Fullerton, CA
Posts: 7,030
Hi Iniamyen,

I havn't done C++ in a while, but as I recall arrays are statically created, so you must have the size set before compiling the code. You might have to use a dynamic data structure like a linked list to store the data.

Edit: doctorgonzo beat me to it.
DrZaius is offline   Reply With Quote
Old 06-23-2004, 12:20 PM   #4
Member (10 bit)
 
Iniamyen's Avatar
 
Join Date: Aug 2003
Location: Seattle, WA
Posts: 517
Send a message via AIM to Iniamyen
Yes, I made an exact counterpart to my array code which does a vector, and it works great, but the boss told me he wanted the object to have an array instead (I don't know why.)

Well, as long as I can make it too big, I know generally how many elements I'll need so I can do it that way.

I did not know that it was such a pain to specify array size at runtime. Thanks very much for the help.
Iniamyen is offline   Reply With Quote
Old 06-23-2004, 12:24 PM   #5
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
Quote:
Originally posted by Iniamyen
Yes, I made an exact counterpart to my array code which does a vector, and it works great, but the boss told me he wanted the object to have an array instead (I don't know why).
Ah, yes, the "I want it my way even if it doesn't work" method of managing.
doctorgonzo is offline   Reply With Quote
Old 06-23-2004, 12:46 PM   #6
Member (10 bit)
 
Iniamyen's Avatar
 
Join Date: Aug 2003
Location: Seattle, WA
Posts: 517
Send a message via AIM to Iniamyen
I don't know if the array provides any performance advantage for large objects or lots of objects or anything like that. The only advantage that I see to using arrays is that they can be multidimensional. I'm not sure if what I'm doing will end up needing multidimensionality or not. I'm sure the specifics of exactly how things are stored in memory and how the computer accesses different types of containers etc... is very interesting, but I don't have the time to work on that.
Iniamyen is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 06:48 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2