Hey,
I am getting a runtime error when I run my removeSomeElement() function. Can someone have a look and tell me what I am doing wrong.
thanks a lot.
My Code is:
My main file is:Code:#include "Person.h" class SetOfPersons{ //Unbounded Container public: SetOfPersons(int n = 4) : index(0),size(n){ buffer = new Person*[n] ;} ~SetOfPersons(void) {delete [] buffer;} void add(Person & item) { if(index == size) { //grow the container cout << "growing\n"; Person ** temp = buffer; buffer = new Person*[size*2]; for(int i=0; i<size; i++) buffer[i] = temp[i]; size = size *2; delete [] *temp; } buffer[index++] = &item; } int sizea(){ return index;} Person & removeSomeElement() { index--; if(index < size/2) { //shrink the container return *buffer[++index]; cout << "shrinking\n"; Person ** temp = buffer; buffer = new Person*[size/2]; for(int i=0; i<index; i++) buffer[i] = temp[i]; size = size/2; delete [] *temp; } } Person & someElement() { if(index>0){ return *buffer[++index]; } else{index=0;} } void printOn(ostream & ostr){ } private: Person ** buffer; int size; int index; }; ostream & operator<<(ostream & ostr, SetOfPersons & p) { p.printOn(ostr); return ostr; }
Code://file main.cpp #include <iostream.h> #include <string.h> #include "Date.h" #include "Person.h" #include "SetOfPersons.h" main() { int i; //create some persons on the heap Person *p1 = new Person("Lou", "lou@chat.ca", 20, 6, 1960); Person *p2 = new Person("Frank", "f123@chat.ca", 20, 3, 1967); Person *p3 = new Person("Ann", "ann@chat.ca", 20, 8, 1960); Person *p4 = new Person(); //Create some sets SetOfPersons boys, girls; //Add some persons to the sets boys.add(*p4); //test to see if the same object is retrieved from the set. if (p1 != &boys.removeSomeElement() ) cout << "ERROR: the objects are different \n"; else cout << "Good, the objects are the same \n"; boys.add( *p1); boys.add( *p2); girls.add( *p3); boys.add(*(new Person("John", "f123@chat.ca", 20, 3, 1967))); girls.add(*(new Person("Sue", "f123@chat.ca", 20, 3, 1967))); boys.add(*(new Person("Frank", "frank@chat.ca", 25, 4, 1958))); girls.add(*(new Person("Mary", "mary@chat.ca", 25, 4, 1955))); boys.add(*(new Person("John", "johnchat.ca", 12, 12, 1970))); //print all the boys using the removeSomeElement() method and delete them int numberOfBoys = boys.sizea(); cout << "number of boys = " << numberOfBoys << "\n"; for(i = 0; i<numberOfBoys; i++) { Person & boy = boys.removeSomeElement(); //cout << boy << "\n"; // delete &boy; } //print the girls using the << operator of the SetOfPersons class //cout << "number of girls = " << girls.sizaa() << "\n"; //cout << girls << "\n"; //print of the girls birthdays and using the someElement() method int numberOfGirls = girls.sizea(); //for(int i = 0; i<numberOfGirls; i++) //cout << girl.someElement().getBirthDate() << "\n"; //delete all the girls from the heap numberOfGirls = girls.sizea(); for(i = 0; i<numberOfGirls; i++) { Person & her = girls.removeSomeElement(); // delete &her; } //end of main }



LinkBack URL
About LinkBacks


