daisypolly
Programmer
Hi,
I am getting an error as soon as I run my program. The error is main.exe has caused a system error and needs to be closed. I know there is something wrong with my Container class but I can't find out the problem.
My Container class
Code:
#include <iostream.h>
//file SetOfPersons.h
class SetOfPersons {
public:
SetOfPersons(int initial_size = 4) {
//default constructor allocate appropriate heap storage
//store elements on heap array declared like this:
//new Person*[initial_size];
cout << "SetOfPersons(int)\n";
numberOfElements = initial_size;
elements = new Person*[initial_size];}
~SetOfPersons(void) {delete [] elements;}
void add(Person & element) {
if(capacity == size()) { //grow the container
cout << "growing\n";
Person **temp = elements;
*elements = new Person[capacity*2];
for(int i=0; i<capacity; i++) *elements = *temp;
numberOfElements = numberOfElements *2;
delete [] *temp;
}
*elements[capacity++] = element;
}
Person & someElement(){
capacity++;
capacity = capacity % numberOfElements;
return *elements[capacity]; }
Person & removeSomeElement() {
// answer some (any) element from the set and remove it from the set
// if the set is more than half empty release some memory
capacity--;
if(capacity < numberOfElements/2) { //shrink the container
cout << "shrinking\n";
Person ** temp = elements;
*elements = new Person[numberOfElements/2];
for(int i=0; i<capacity; i++) elements = temp;
numberOfElements = numberOfElements/2;
delete [] temp;
}
return *elements[capacity];
}
int size(){
//answer the number of elements in the set.
//not just the size of the array
return numberOfElements;
}
void printOn(ostream & ostr){
int i ;
for( i = 0; i<size(); i++) {
cout<<*elements<<"\n";
}
}
private:
Person ** elements;
int numberOfElements; //number of elements in the set
int capacity; //size of the available array memory
};
//you will need the following global operator to print Sets.
//Put this in the same file as class Set, but not inside the Set class.
ostream & operator<<(ostream & ostr, SetOfPersons & s) {
s.printOn(ostr);
return ostr;
}
I am getting an error as soon as I run my program. The error is main.exe has caused a system error and needs to be closed. I know there is something wrong with my Container class but I can't find out the problem.
My Container class
Code:
#include <iostream.h>
//file SetOfPersons.h
class SetOfPersons {
public:
SetOfPersons(int initial_size = 4) {
//default constructor allocate appropriate heap storage
//store elements on heap array declared like this:
//new Person*[initial_size];
cout << "SetOfPersons(int)\n";
numberOfElements = initial_size;
elements = new Person*[initial_size];}
~SetOfPersons(void) {delete [] elements;}
void add(Person & element) {
if(capacity == size()) { //grow the container
cout << "growing\n";
Person **temp = elements;
*elements = new Person[capacity*2];
for(int i=0; i<capacity; i++) *elements = *temp;
numberOfElements = numberOfElements *2;
delete [] *temp;
}
*elements[capacity++] = element;
}
Person & someElement(){
capacity++;
capacity = capacity % numberOfElements;
return *elements[capacity]; }
Person & removeSomeElement() {
// answer some (any) element from the set and remove it from the set
// if the set is more than half empty release some memory
capacity--;
if(capacity < numberOfElements/2) { //shrink the container
cout << "shrinking\n";
Person ** temp = elements;
*elements = new Person[numberOfElements/2];
for(int i=0; i<capacity; i++) elements = temp;
numberOfElements = numberOfElements/2;
delete [] temp;
}
return *elements[capacity];
}
int size(){
//answer the number of elements in the set.
//not just the size of the array
return numberOfElements;
}
void printOn(ostream & ostr){
int i ;
for( i = 0; i<size(); i++) {
cout<<*elements<<"\n";
}
}
private:
Person ** elements;
int numberOfElements; //number of elements in the set
int capacity; //size of the available array memory
};
//you will need the following global operator to print Sets.
//Put this in the same file as class Set, but not inside the Set class.
ostream & operator<<(ostream & ostr, SetOfPersons & s) {
s.printOn(ostr);
return ostr;
}