该程序编译并运行,但是当我输入 2 个或更多条目时,它在我输入第二个电话号码时立即崩溃。这是我输入的内容和调用堆栈:
“Library Holdings.exe 中 0x013A6EC6 处的未处理异常:0xC0000005:访问冲突读取位置 0x00000000。”
这次它在我进入第二个电话之前就崩溃了#。
这是发生的另一个崩溃,由于某种原因在不同的点崩溃,但这是我遇到的最常见的崩溃
Library Holdings.exe 中 0x779B016E (ntdll.dll) 处未处理的异常:0x00000000:操作成功完成。
我想知道如何让它停止崩溃?
图书馆驱动程序.h:
#ifndef _LIBRARYDRV_H
#define _LIBRARYDRV_H
#include "Holding.h"
#include "Recording.h"
#include "Book.h"
Holding* inputHolding();
#endif
图书馆博士.cpp:
#include <iostream>
#include "LibraryDrv.h"
using namespace std;
int main() {
Holding *hptr[5];
for (int i = 0; i < 5; i++) {
hptr[i] = inputHolding();
}
for (int i = 0; i < 5; i++) {
hptr[i]->print();
}
return 0;
}
Holding* inputHolding() {
char selection;
char title[50];
int callNumber = 0;
char author[50];
char performer[50];
char format;
cout << "Enter B for book, R for recording: ";
cin >> selection;
if (selection == 'B') {
cout << "Enter book title: ";
cin >> title;
cout << "Enter book author: ";
cin >> author;
cout << "Enter call number: ";
cin >> callNumber;
Book* aBook = new Book(title, callNumber, author);
return aBook;
}
else if (selection == 'R') {
cout << "Enter recording title: ";
cin >> title;
cout << "Enter performer: ";
cin >> performer;
cout << "Enter format: (M)P3, (W)AV, (A)IFF: ";
cin >> format;
cout << "Enter call number: ";
cin >> callNumber;
Recording* aRecording = new Recording(title, callNumber, performer, format);
return aRecording;
}
else {
cout << "Incorrect selection" << endl;
return nullptr;
}
}
控股.h
#ifndef _HOLDING_H
#define _HOLDING_H
class Holding {
protected:
int callNumber;
char* title;
public:
Holding();
Holding(const Holding&);
Holding(char*, int);
virtual void print() = 0;
virtual ~Holding();
};
#endif
控股.cpp
#include "Holding.h"
#include "String.h"
Holding::Holding() {
}
Holding::Holding(const Holding& copy) {
title = new char[strlen(copy.title) + 1];
strcpy_s(title,sizeof(title), copy.title);
callNumber = copy.callNumber;
}
Holding::Holding(char* copy, int inputCall) {
int len = strlen(copy) + 1;
title = new char[len];
strcpy_s(title, sizeof(char) * len, copy);
callNumber = inputCall;
}
Holding::~Holding() {
delete [] title;
}
书.h:
#ifndef _BOOK_H
#define _BOOK_H
#include "Holding.h"
class Book : public Holding {
private:
char* author;
public:
Book();
Book(const Book&);
Book(char*, int, char*);
virtual void print();
virtual ~Book();
};
#endif
书本.cpp
#include <iostream>
#include "Holding.h"
#include "Book.h"
#include "String.h"
using namespace std;
Book::Book() {
author = nullptr;
}
Book::Book(const Book& copy) : Holding(copy) {
author = new char[strlen(copy.author) + 1];
strcpy_s(author, sizeof(author), copy.author);
}
Book::Book(char* inputTitle, int inputCallNum, char* inputAuthor) : Holding(inputTitle, inputCallNum) {
int len = strlen(inputAuthor) + 1;
author = new char[len];
strcpy_s(author, sizeof(author)*len, inputAuthor);
}
Book::~Book() {
delete [] author;
}
void Book::print() {
cout << "BOOK: " << author << " " << title << " " << callNumber << endl;
}
录音.h:
#ifndef _RECORDING_H
#define _RECORDING_H
#include "Holding.h"
class Recording : public Holding {
private:
char* performer;
char format;
public:
Recording();
Recording(const Recording&);
Recording(char*, int, char*, char);
virtual void print();
virtual ~Recording();
};
#endif
录音.cpp:
#include <iostream>
#include "String.h"
#include "Holding.h"
#include "Recording.h"
using namespace std;
Recording::Recording() {
}
Recording::Recording(const Recording& copy) : Holding(copy) {
int len = strlen(copy.performer) + 1;
performer = new char[len];
strcat_s(performer, sizeof(performer)*len, copy.performer);
format = copy.format;
}
Recording::Recording(char* inputTitle, int inputCallNum, char* inputPerformer, char inputFormat)
: Holding(inputTitle, inputCallNum) {
int len = strlen(inputPerformer) + 1;
performer = new char[len];
strcpy_s(performer, sizeof(performer)*len, inputPerformer);
format = inputFormat;
}
Recording::~Recording() {
delete [] performer;
}
void Recording::print() {
cout << "RECORDING: " << title << " " << performer << " (" << format << ") " << callNumber << endl;
}