|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
#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** |
|
#2
|
||||
|
||||
|
Re: my class constructor not constructing
Quote:
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
}
|
|
#3
|
||||
|
||||
|
>>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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > my class constructor not constructing |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|