在我的主要函数中,我调用inputHolding 5次。它经历了几个循环,然后给出了一个错误:当堆试图在callNumber中读取时(通常是在第三个循环上),堆已经损坏。我怎样才能解决这场车祸?
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* muhbooksie = new Book(title, callNumber, author);
return muhbooksie;
}
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* muhbooksie = new Recording(title, callNumber, performer, format);
return muhbooksie;
}
else {
cout << "Incorrect selection" << endl;
return nullptr;
}
}Book.cpp:
#include <iostream>
#include "Holding.h"
#include "Book.h"
#include "String.h"
using namespace std;
Book::Book() {
}
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;
}发布于 2013-07-26 12:19:33
在问题的注释部分中,您应该(对于Book类):
这样,您的代码将更加简洁,例如,如果使用默认构造函数,析构函数将不会删除未指定的作者。
https://stackoverflow.com/questions/17876494
复制相似问题