This is for a school project. I have a class called buddy and a linked list buddyList. While debugging, I encountered some nullptr exceptions that I think I fixed correctly, but last I tried to debug, I got this exception. What can I do to fix this? Keep in mind some of this code was provided via the instructor and to goal of the project was to add specific methods to the existing classes. Thanks!
//buddy.h
#pragma once
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class buddy {
friend class buddyList;
public:
buddy() {
first = last = phone = ""; next = NULL;
cout << "Buddy allocated\n";
}
~buddy() {
if (next != NULL) {
delete next;
cout << "Buddy " << first << " " << last << " deallocated!\n";
}
}
void set(string fname, string lname, string tele) {
first = fname;
last = lname;
phone = tele;
}
void print() {
cout << left << setw(15) << last << setw(15) << first << " " << phone << endl;
}
private:
string first;
string last;
string phone;
buddy * next;
};
//buddyList.h
#pragma once
#include <string>
#include "buddy.h"
using namespace std;
class buddyList {
public:
buddyList();
~buddyList();
void add(string fname, string lname, string phone);
void print();
int drop(string fname, string lname);
void sort();
void read();
private:
buddy * head;
buddy* search(string fname, string lname);
buddy* maxByName();
void remove(buddy * r);
void add(buddy * n);
};
//buddyList.cpp
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "buddyList.h"
//------------------------------------------------------------------
// constructor and destructor
//------------------------------------------------------------------
buddyList::buddyList() {
head = NULL;
cout << "List allocated!\n";
}
buddyList::~buddyList() {
if (head != NULL)
delete head;
cout << "List destroyed!\n";
}
//------------------------------------------------------------------
// add
//------------------------------------------------------------------
void buddyList::add(string fname, string lname, string phone) {
buddy * b = new buddy;
b->first = fname;
b->last = lname;
b->phone = phone;
add(b);
}
void buddyList::add(buddy * p) {
p->next = head;//Error here
head = p;
}
//------------------------------------------------------------------
// print
//------------------------------------------------------------------
void buddyList::print() {
cout << "\nBuddy List: =========================================\n";
buddy * p = head;
while (p != NULL) {
p->print();
p = p->next;
}
cout << "======================================================\n\n";
delete p;
p = NULL;
}
//------------------------------------------------------------------
// search
//------------------------------------------------------------------
buddy* buddyList::search(string fname, string lname) {
buddy * p = head;
while (p != NULL) {
if (p->first == fname && p->last == lname)
return p;
p = p->next;
}
delete p;
p = NULL;
return NULL;
}
//------------------------------------------------------------------
// read
//------------------------------------------------------------------
void buddyList::read() {
ifstream f;
f.open("buddyList.txt");
if (f.fail()) {
cout << "Error opening input file!\n";
return;
}
string fname, lname, tele;
while (!f.eof()) {
f >> fname >> lname >> tele;
add(fname, lname, tele);
}
f.close();
}
//------------------------------------------------------------------
// maxByName
//------------------------------------------------------------------
buddy* buddyList::maxByName() {
buddy * p = head;
if (p == NULL)
return NULL;
buddy * q = p->next;
while (q != NULL) {
if (p->last > q->last) {
p = q;
}
else if (p->last == q->last)
if (p->first > q->first)
p = q;
q = q->next;
}
return p;
}
//------------------------------------------------------------------
// remove
//------------------------------------------------------------------
void buddyList::remove(buddy * r) {
if (head == NULL)
return;
if (r == NULL)
return;
if (r == head)
head = head->next;
else {
buddy * b4 = head;
while (b4->next != r && b4 != NULL) {
b4 = b4->next;
}
if (b4 == NULL)
return;
b4->next = r->next;
}
r->next = NULL;
delete r;
r = NULL;
}
//------------------------------------------------------------------
// drop
//------------------------------------------------------------------
int buddyList::drop(string fname, string lname) {
buddy * p = search(fname, lname);
if (p == NULL)
return -1;
else {
remove(p);
return 0;
}
}
//------------------------------------------------------------------
// sort
//------------------------------------------------------------------
void buddyList::sort() {
buddyList tempList;
buddy * p = head;
while (p != NULL) {
buddy * q = maxByName();
remove(q);
tempList.add(q);
}
delete p;
head = tempList.head;
tempList.head = NULL;
}