该代码由四部分组成。其中一个是DVD类,其他的是HarryPotterDVD.h,HarryPotterDVD.cpp和HarryPotterDVDDriver.cpp。当我编译代码时,它在编译时不会给我任何错误,但是当我尝试运行已编译的可执行文件时,出现了Abort(核心转储)错误。我认为这是因为我使用继承函数的方式。你们可以帮我弄清楚如何解决问题。
DVD.cpp
/admin/application_settings/network
HarryPotterDVD.h
#include "DVD.h"
DVD::DVD() {
id =0;
title = new char[1];
title[0] = '\0';
director = new char[1];
director[0] = '\0';
}
DVD::DVD(unsigned int i, const char* t, const char* dir) {
id = i;
int len;
for(len = 0; t[len] !='\0'; ++len);
title = new char[len+1];
for(int i = 0; i < len+1; i++) {title[i]=t[i];}
int len2;
for(len2=0; dir[len2] !='\0'; ++len2);
director = new char[len2+1];
for(int j = 0; j<len2+1; j++) {director[j] = dir[j];}
}
DVD::DVD(const DVD &d): id(d.id) {
int len;
for(len=0; d.title[len] !='\0'; ++len);
title = new char[len+1];
for(int j = 0; j < len+1; j++) {
title[j] = d.title[j];
}
int len2;
for(len2=0; d.director[len2] != '\0'; ++len2);
director = new char[len2+1];
for(int j = 0; j<len2 +1; j++) {
director[j] = d.director[j];
}
}
DVD::~DVD() {
if (title) {
delete [] title;
}
if (director) {
delete [] director;
}
}
int DVD::getID() {
return id;
}
char* DVD::getTitle() {
return title;
}
char* DVD::getDirector() {
return director;
}
void DVD::display() {
cout << '[' << id << '.' << ' ' << title << '/' << director << ']' << endl;
}
void DVD::setID(unsigned int i) {
id = i;
}
void DVD::setTitle(const char* t) {
delete [] title;
int len;
for(len = 0; t[len] !='\0'; ++len);
title = new char[len+1];
for(int i = 0; i < len+1; i++) {title[i]=t[i];}
}
void DVD::setDirector(const char* dir) {
delete [] director;
int len2;
for(len2=0; dir[len2] !='\0'; ++len2);
director = new char[len2+1];
for(int j = 0; j<len2+1; j++) {director[j] = dir[j];}
}
DVD& DVD::operator= (const DVD& arg) {
id = arg.id;
int len;
for(len = 0; arg.title[len] !='\0'; ++len);
title = new char[len+1];
for(int i = 0; i < len+1; i++) {title[i]=arg.title[i];}
int len2;
for(len2=0; arg.director[len2] !='\0'; ++len2);
director = new char[len2+1];
for(int j = 0; j<len2+1; j++) {director[j] = arg.director[j];}
return *this;
}
HarryPotterDVD.cpp
#ifndef _HarryPotterDVD_
#define _HarryPotterDVD_
#include<iostream>
#include "DVD.h"
using namespace std;
class HarryPotterDVD : public DVD {
int episode;
char * antagonist;
public:
HarryPotterDVD(unsigned int i, char* t, char* dir, int n, char* ant);
HarryPotterDVD();
HarryPotterDVD(HarryPotterDVD& d);
~HarryPotterDVD();
int getEpisode();
char* getAntagonist();
void display();
void setEpisode(unsigned int e);
void setAntagonist(const char* c);
HarryPotterDVD& operator= (HarryPotterDVD& arg);
};
#endif
HarryPotterDVDDriver.cpp
#include "HarryPotterDVD.h"
#include<iostream>
using namespace std;
HarryPotterDVD::HarryPotterDVD(unsigned int i, char* t, char* dir, int n, char* ant): DVD( i , t , dir) {
episode = n;
int len;
for(len = 0; ant[len] !='\0'; ++len);
antagonist = new char[len+1];
for(int j = 0; j < len+1; j++) {antagonist[j]=ant[j];}
}
HarryPotterDVD::HarryPotterDVD(HarryPotterDVD &d) {
DVD::setID(d.DVD::getID());
DVD::setTitle(d.DVD::getTitle());
DVD::setDirector(d.DVD::getDirector());
episode = d.episode;
int len;
for(len = 0; d.antagonist[len] != '\0'; len++);
antagonist = new char[len + 1];
for(int i = 0; i < len; i++){
antagonist[i] = d.antagonist[i];
}
antagonist[len+1] = '\0';
}
HarryPotterDVD::HarryPotterDVD() {
episode = -1;
char* tmp = "";
antagonist = tmp;
}
HarryPotterDVD::~HarryPotterDVD(){
delete [] antagonist;
}
int HarryPotterDVD::getEpisode(){
return episode;
}
char * HarryPotterDVD::getAntagonist(){
return antagonist;
}
void HarryPotterDVD::display(){
cout << "[" << DVD::getID() << ". HP" << episode;
cout << ":" << DVD::getTitle() << "/" << DVD::getDirector();
cout << "/" << antagonist << "]" << endl;
}
void HarryPotterDVD::setEpisode(unsigned int e){
episode = e;
}
void HarryPotterDVD::setAntagonist(const char * c){
delete [] antagonist;
int len;
for(len = 0; c[len] != '\0'; len++);
antagonist = new char[len + 1];
for(int i = 0; i < len; i++){
antagonist[i] = c[i];
}
antagonist[len+1] = '\0';
}
HarryPotterDVD& HarryPotterDVD::operator= (HarryPotterDVD &d){
delete [] antagonist;
episode = d.episode;
int len3;
for(len3 = 0; d.antagonist[len3] != '\0'; len3++);
antagonist = new char[len3 + 1];
for(int i = 0; i < len3; i++){
antagonist[i] = d.antagonist[i];
}
antagonist[len3+1] = '\0';
DVD::operator=(d);
/*
setID(d.getID());
int len;
for(len = 0; d.getTitle()[len] != '\0'; len++);
char * tit = new char[len+1];
for(int i = 0; i < len; i++){
tit[i] = d.getTitle()[i];
}
tit[len] = '\0';
setTitle(tit);
int len2;
for(len2 = 0; d.getDirector()[len2] != '\0'; len2++);
char* dire = new char[len2+1];
for(int i = 0; i < len2; i++){
dire[i] = d.getDirector()[i];
}
dire[len2] = '\0';
setDirector(dire);
*/
return* this;
}
答案 0 :(得分:0)
在HarryPotterDVD
的默认构造函数中,您正在向成员变量antagonist
分配一个随机指针,该成员变量不是用new[]
创建的
HarryPotterDVD::HarryPotterDVD() {
episode = -1;
char* tmp = "";
antagonist = tmp;
}
在operator=()
函数中,当执行语句h1=h3;
时,您正在使用antagonist
删除delete[]
,这会导致分段错误。
delete
是null安全的。因此,在您的默认构造函数中,您只需在分配期间将它们的nullptr
和delete
的指针指向它们就不会引起任何错误。如果您以这种方式进行更改,那么您的问题应该会消失。
HarryPotterDVD::HarryPotterDVD() {
episode = -1;
antagonist = nullptr;
}