Question: Exception thrown: read access violation. this was nullptr. Person.h FILE #pragma once #define PERSON_H #include using namespace std; class Person { private: string name; int

Exception thrown: read access violation. this was nullptr.

Person.h FILE

#pragma once #define PERSON_H #include

using namespace std;

class Person { private: string name; int age; double weight;

public: Person(string, int, double); Person(); void setName(string); void setAge(int); void setWeight(double); string getName(); int getAge(); double getWeight(); };

Person.cpp FILE

#include "Person.h" #include

Person::Person(string x, int y, double z) { this->name = x; this->age = y; this->weight = z; } Person::Person() { this->name = ""; this->age = 0; this->weight = 0.0; }

void Person::setName(string nameOne) { this->name = nameOne; }

void Person::setAge(int ageOne) { this->age = ageOne; }

void Person::setWeight(double weightOne) { this->weight = weightOne; }

string Person::getName() { return this->name; } int Person::getAge() { return this->age; }

double Person::getWeight() { return this->weight; }

MAIN.CPP FILE

#include #include "Person.h" #include

using namespace std;

class PersonData { private: Person tempPerson; Person* personList; int capacity, size;

public: PersonData(Person x) { this->tempPerson = x; this->size = 0; } ~PersonData() { delete[] personList; personList = nullptr; } void resizeArray() { capacity++;

Person* tempArray = new Person[capacity];

for (int counter = 0; counter < this->capacity; counter++) {

tempArray[counter] = this->personList[counter];

}

delete[] this->personList;

this->personList = tempArray; } void addPerson() { this->capacity = 1;

if (this->capacity <= this->size) { resizeArray(); }

this->personList[size] = this->tempPerson;

this->size += 1; }

void listPersons() {

if (size > 0) { for (int i = 0; i < size; i++) {

cout << "Name: " << personList[i].getName() << endl; cout << "Age: " << personList[i].getAge() << endl; cout << "Weight: " << personList[i].getWeight() << endl << endl;

cout << "Capacity of the array: " << capacity << endl; cout << "Records in the array: " << size << endl;

} } else { cout << "Capacity of the array: " << capacity << endl; cout << "Records in the array: " << size << endl; } }

};

int main() { Person personOne("John", 22, 140.6); PersonData person1(personOne); person1.addPerson();

person1.listPersons();

Person personTwo("Mike", 55, 147.6); }

Where is the nullptr, and how can I fix the issue.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!