Lỗi break point khi dùng cin - Kế thừa - Container trong C++

Mình có 3 class sau: Class Record, Class Check_Record, Class Online_Record, Class Check_record và online_record kế thừa public từ Record.

Sau đó mình khai báo Class Container List_record để chứa, List_record trỏ đến Class Record.
Tiếp theo mình tạo hàm thêm Record mới cho List_record có sẵn, nhưng khi chạy thì Compiler báo Break Point. đây là code của mình:

Class Record:

class record {
protected:
	string sport;
	string discipline;
	string type;
	string gender;
	int date;
	string name;
	string country;
	int achivement;
public:
	record() : sport(""), discipline(""), type(""), gender(""),
		date(0), name(""), country(""), achivement(0) {};
	record(string, string, string, string, int, string, string, int);
	friend bool operator ==(record another1, record another2);
	virtual void read_record_from_file(ifstream& file);
	virtual void write_record_to_file(ofstream& file);
	virtual void print_record();
	virtual void read_record(); // Đây là hàm thêm record mới
	string get_sport();
	string get_gender();
	int get_date();
};
void record::read_record() {
	cout << "sport : " << endl;
	cin >> sport;                         // Báo Break Point tại đây (has triggered a breakpoint) 
	
	cout << "discipline : " << endl;
	cin >> discipline;
	
	cout << "type : " << endl;
	cin >> type;

	cout << "gender : " << endl;
	cin >> gender;
	
	cout << "date : " << endl;
	cin >> date;
	
	cout << "name : " << endl;
	fflush(stdin);
	cin >> name;
	
	cout << "country : " << endl;
	cin >> country;
	
	cout << "achivement : " << endl;
	cin >> achivement;
	fflush(stdin);
}

Class List_record:

class list_record
{
private:
	int max_num_record;
	int num_record;
	int num_check;
	int num_online;
	record **precords = new record*[max_num_record]; //Con trỏ tạo vùng nhớ lưu các đối tượng
public:
	list_record(int max_human);
	~list_record();

	void operator +=(record *arecord); // Operator thêm đối tượng mới
	void show();
	void read_record_from_file(string filename);
	void write_record_to_file(string filename);
	void find1(string);
	void find2(string, int);
};
void list_record::operator +=(record* arecord) {
	if (num_record < max_num_record) {
		precords[num_record] = arecord;
		num_record++;
	}
}

Hàm main của mình:

list_record alist_record(20); // tạo 20 ô nhớ, nhưng đang lưu 14 ô - dữ liệu đọc từ file
int ch2;
cin >> ch2;
if (ch2 == 1) {
	record* newrecord;
	newrecord = new record;
	newrecord->read_record();
	alist_record += newrecord ;
}

Nhìn sơ thì class của bạn có member con trỏ nên cần copy constructordestructor.

1 Like

Mình có khai báo constructor vs destructor rồi đấy. Nó chỉ báo lỗi ở Cin, mình Debug thì chạy bình thường, còn Run thì bị break point :((

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