Gỡ lỗi Unhandled exception at 0x570040C9 trong C++

mình viết code bằng visual studio 2019. khi chạy nó không hiển thị kết quả, và mình chạy debug thì nó báo lỗi .

Unhandled exception at 0x570040C9 (vcruntime140d.dll) in class_and_object.exe: 0xC0000005: Access violation writing location 0xDDDDDDDD.

ai rành chỉ giúp mình gỡ lỗi vs ạ

#include <string>
#include <iostream>
using namespace std;

class person
{
private:
	int age;
        int pId;
	string firstName;
	string lastName;
	string midName;
	string address;
	string phoneNumber;
	string gender;

public:
	static int id;
	person();
	void getinformation(int, int, string, string, string, string, string, string);

	int getID();
	void setID(int);

	int getAge();
	void setAge(int);

	string getFirstName();
	void setFirstName(string);

	string getMidName();
	void setMidName(string);

	string getLastName();
	void setLastName(string);

	string getAddress();
	void setAddress(string);

	string getPhoneNumber();
	void setPhoneNumber(string);

	string getGender();
	void setGender(string);

	void showInFo();

	string fullName();
};

person::person() {
	this->pId = 0;
	this->age = 0;
	this->address = "";
	this->firstName = "";
	this->gender = "";
	this->lastName = "";
	this->midName = "";
	this->phoneNumber = "";
};

void person::getinformation(int id, int age, string address,
	string firstName, string gender, string lastName, string midName, string phoneNumber) {
	setID(id);
	setAge(age);
	setAddress(address);
	setFirstName(firstName);
	setGender(gender);
	setLastName(lastName);
	setMidName(midName);
	setPhoneNumber(phoneNumber);
}

int person::id = 100;

void person::setID(int id) {
	this->pId = (id >= 100) ? id : person::id++;
}
void person::setAge(int age) {
	this->age = (age >= 0) ? age : 0;
}
void person::setFirstName(string firstName) {
	this->firstName = firstName;
}
void person::setMidName(string midName) {
	this->midName = midName;
}
void person::setLastName(string lastName) {
	this->lastName = lastName;
}
void person::setAddress(string address) {
	this->address = address;
}
void person::setPhoneNumber(string phoneNumber) {
	this->phoneNumber = phoneNumber;
}
void person::setGender(string gender) {
	this->gender = gender;
}

int person::getID() {
	return this->pId;
}
int person::getAge() {
	return this->age;
}
string person::getFirstName() {
	return this->firstName;
}
string person::getMidName() {
	return this->midName;
}
string person::getLastName() {
	return this->lastName;
}
string person::getAddress() {
	return this->address;
}
string person::getPhoneNumber() {
	return this->phoneNumber;
}
string person::getGender() {
	return this->gender;
}

string person::fullName() {
	return getFirstName() + " " + getMidName() + " " + getLastName();
}

void person::showInFo() {
	cout << "===============================================" << endl;
	cout << "Id: " << getID() << endl//co the truyen truc tiep thuat tinh id ma ko can ham get
		<< "full name: " << fullName() << endl
		<< "age: " << getAge() << endl
		<< "address: " << getAddress() << endl
		<< "gender: " << getGender() << endl;
	cout << "===============================================" << endl;
}

void getPepleInfo(person* pp, size_t n) {
	int id, age;
	string first, mid, last, address, phone, gender;
	for (size_t i = 1; i <= n; i++)
	{
		cout << "Enter the information of the " << i << "st person" << endl;
		cout << "first name: ";
		cin >> first;
	
		cout << "midle name: ";
		cin >> mid;
		cout << "last name: ";
		cin >> last;
		
		cout << "address: ";
		cin >> address;
		cout << "age: ";
		cin >> age;
		cout << "phone number: ";
		cin >> phone;
		cout << "gender: ";
		cin >> gender;

		pp[i].getinformation(0, age, address, first, gender, last, mid, phone);
	}

}

void showPeople(person* pp, size_t n) {
	for (size_t i = 1; i <= n; i++)
	{
		pp[i].showInFo();
	}
}

int main() {

	size_t n;
	cout << "enter the number people: ";
	cin >> n;
	//cap phat
	person* people = new person[n];
	//nhap du lieu
	getPepleInfo(people, n);
	//xuat du lieu

	showPeople(people, n);

	return 0;
}

Chắc lỗi tương đương với NullPointerException ở một vài ngôn ngữ nào đó.

Bạn đã học đến lớp (class) và đối tượng (object) thì chắc cũng biết từ khóa new rồi nhỉ.
Mình đoán không sai thì lỗi xuất hiện sau khi bạn nhập đầy đủ thông tin cho phần tử person đầu tiên của mảng.
Đã là đối tượng của lớp thì không thể thiếu new.

2 Likes

vậy giờ mình sửa sao zay, mình chỉ ms học class thôi à. chưa biết nhiều

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?