プログラムはコンパイルされて実行されますが、2 つ以上のエントリを入力すると、2 つ目の請求番号を入力した瞬間にクラッシュします。ここに私が入力したものとコールスタックがあります:
「ライブラリ Holdings.exe の 0x013A6EC6 で未処理の例外: 0xC0000005: アクセス違反の読み取り場所 0x00000000」。
今回は、2 番目の呼び出しに入る前にクラッシュしました #.
これは発生した別のクラッシュです。何らかの理由でさまざまなポイントでクラッシュしますが、これは私が経験した最も一般的なクラッシュでした
ライブラリ Holdings.exe の 0x779B016E (ntdll.dll) で未処理の例外: 0x00000000: 操作は正常に完了しました。
クラッシュを止めるにはどうすればよいですか?
LibraryDrv.h:
#ifndef _LIBRARYDRV_H
#define _LIBRARYDRV_H
#include "Holding.h"
#include "Recording.h"
#include "Book.h"
Holding* inputHolding();
#endif
LibraryDr.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
Book.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;
}
Recording.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
Recording.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;
}