C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 10th, 2003, 10:31 PM
maskzilla maskzilla is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Posts: 11 maskzilla User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question my class constructor not constructing

#include "accounts.h"

class Bank{
sav_acct *Ps;
chk_acct *Pc;
time_acct *Pt;
char name[20];
char addy[50];
public:
Bank(int x, int y, int z){
Ps = new sav_acct[x];
Pc = new chk_acct[y];
Pt = new time_acct[z];
}

~Bank(){
delete []Ps;
delete []Pc;
delete []Pt;
}

void set_name(char *p);
char *get_name(){ return name;}
void withdrawl(char type, int index, double amt);
void deposit(char type, int index, double amt);
double cash_chk(int index, double amt);


};

okay the main problem is, when i call the constructor
for example:
Bank b1( 200, 400, 500);

it does not make 200, 400, or 500 of those instances, i have a feeling it is because of the way i have the dynamic memory created...
any ideas?
**the sav, check, and time accounts are in accounts.h**

Reply With Quote
  #2  
Old February 10th, 2003, 10:49 PM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 24 sec
Reputation Power: 9
Re: my class constructor not constructing

Quote:
Originally posted by maskzilla
it does not make 200, 400, or 500 of those instances, i have a feeling it is because of the way i have the dynamic memory created...


The number of elements of an array, the array bound, must be a constant expression. If you need variable bounds, use a vector. For example:

Code:
void func(int i)
{
    int v[i];   // error: not a constant
    vector<int> v2(i);  // ok
}

Reply With Quote
  #3  
Old February 11th, 2003, 08:19 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is online now
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,679 Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 42 m 39 sec
Reputation Power: 1083
>>The number of elements of an array, the array bound, must be a constant expression.
He's using the new keyword to allocate the arrays dynamically and assigning them to his pointers. The C++ new keyword allows you to pass a variable in as argument. The following is perfectly legal C++
Code:
  int i;  
  cin >> i;
  int *array = new int[i];
  // Do whatever with array
  delete [] array;


maskzilla, your code looks ok at first sight. The only reason I can think of as to why it might fail, is if there isn't enough RAM. How are you sure that you're not getting 200 elements of sav_acct?

As a side-note, the GNU C and C++ compilers have extensions to the standard that allow you to do this:
Code:
  int i;  
  cin >> i;
  int array[i];
  // use array for whatever

Mind you, this is a non-standard thing though and will not work with other compilers. See http://web.mit.edu/6.033/labdoc/gcc_7.html#IDX275 for the documentation on this and other extensions added to the GNU compilers for C and C++.

Last edited by Scorpions4ever : February 11th, 2003 at 08:44 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > my class constructor not constructing


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT