I have a run time error when I instantiate an object in a PhoneBook class which is an array of doubly linked lists. The code is shown below and the error one line after a comment which says ERROR BELOW. I would appreciate any help. This is just a personal exercise I am doing that I obtained from the internet. Thank you.
#include <iostream>
#include <stdlib.h>
using namespace std;
class Person
{
public:
Person(const char name[], const char phone[])
{
if (strlen(name) <= NAMELEN) { strcpy(this->name, name); }
else { cout << "name was greater than size limit of 15 char" << endl; }
if (strlen(phone) <= PHONELEN) { strcpy(this->phone, phone); }
else { cout << "name was greater than size limit of 10 char" << endl; }
}
bool samePhone(const Person *other) const
{
if (strcmp(this->name, other->getName()) == 0) { return true; } else { return false; }
}
int Person::hashValue() const
{
char cDig1, cDig2;
cDig1 = phone[8];
cDig2 = phone[9];
int dig1, dig2, result;
dig1 = int(cDig1 - '0');
dig2 = int(cDig2 - '0');
result = 10*dig1 + dig2;
return result%101;
}
const char* getName() const
{
return name;
}
enum { NAMELEN = 81, PHONELEN = 10 };
protected:
char name[NAMELEN]; char phone[PHONELEN]; };
class PersonList
{
private:
static int count;
char name[Person::NAMELEN]; char * name2;
char phone[Person::PHONELEN];
Person * aPerson;
struct PersonNode
{
Person *person;
PersonNode *prev, *next;
PersonNode::PersonNode(Person *pers, PersonNode *n, PersonNode *p)
: person(pers), next(n), prev(p)
{ }
} *front, *back;
PersonList(const PersonList&) {}
void operator=(const PersonList&) {}
bool empty() const { return ( !front || !back ); }
public:
PersonList()
:front(NULL), back(NULL)
{
char * tempName = "JoeSmith";
char * tempPhone = "1234561234";
strcpy(name, tempName);
strcpy(phone, tempPhone);
aPerson = new Person(name, phone);
}
~PersonList()
{
while(front)
{
PersonNode * temp = front;
front = front->next;
delete temp;
}
}
void add(Person *new_person)
{
front = new PersonNode(new_person, front , NULL); count++;
if(front->next)
{
front->next->prev = front;
}
if ( empty() )
{
back = front;
}
}
Person *lookup(const Person *target)
{
PersonNode * current = front;
while(current)
{
if(current->person->samePhone(target))
{
if (current==front)
{
return current->person;
}
else if (current == back)
{
PersonNode * objectToMove = current;
back = current->prev; current->prev->next = NULL; front->prev = objectToMove; objectToMove->prev = NULL; objectToMove->next = front; front = objectToMove; return objectToMove->person;
}
else
{
PersonNode * objectToMove = current;
current->prev->next = current->next; current->next->prev = current->prev; front->prev = objectToMove; objectToMove->prev = NULL; objectToMove->next = front; front = objectToMove; return objectToMove->person;
}
} current = current->next;
} return NULL;
}
void remove(const Person *target)
{
PersonNode * current = front;
while(current)
{ if(current->person == target )
{
if (current==front)
{
current->next->prev = NULL;
front = current->next;
delete current;
count--;
break;
}
else if (current == back)
{
current->prev->next = NULL;
back = current->prev;
delete current;
count--;
break;
}
else
{
current->prev->next = current->next;
current->next->prev = current->prev;
delete current;
count--;
break;
}
} current = current->next;
} }
int length() const
{
return count;
}
};
int PersonList::count = 0;
class RevPhoneBook
{
private:
static int count;
PersonList * personListPtr[101];
PersonList oneList;
public:
RevPhoneBook()
{}
~RevPhoneBook()
{
}
void add(Person* new_person)
{
int locationOfPerson = new_person->hashValue();
count++;
personListPtr[locationOfPerson]->add(new_person);
}
Person* lookup(const Person* target)
{
for (int j=0; j<count; ++j) {
if(personListPtr[j]->lookup(target))
{
return personListPtr[j]->lookup(target);
}
}
}
void remove(const Person* target)
{
for (int j=0; j<count; ++j) {
if(personListPtr[j]->lookup(target) == target)
{
personListPtr[j]->remove(target);
}
}
}
};
int RevPhoneBook::count = 0;
int main()
{
Person adrian("adrian", "9565420346");
Person carlos("carlos", "7171234567");
Person tom("tom", "9721234567");
Person jack("jack", "6821234567");
Person miguel("miguel", "9561234567");
RevPhoneBook myPhoneBook;
myPhoneBook.add(&adrian);
return 1;
}