Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get an Error as soon as the program runs.

Status
Not open for further replies.

daisypolly

Programmer
Apr 28, 2005
38
CA
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;
}
 
1. Use CODE tag to present snippets (see Process TGML link on the form).
2. Use <iostream>, not deprecated <iostream.h>
3. Initialize capacity member in the constructor then continue to debug.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top