我需要知道为什么这个程序在编译时会被终止。我不明白终止背后的原因是什么。 请告诉我问题他们的解决方案。 感谢。
#include<iostream.h>
class citizen{
private:
char* name;
char* nationality;
public:
citizen(){
name = "No name is given";
nationality = "No nationality is given\n";
}
citizen(char* name, char* nationality){
this->name = name;
this->nationality = nationality;
}
void display(){
cout << "Name: " << name << endl;
cout << "Nationality: " << nationality << endl;
}
~citizen(){
if(name){
cout << endl << "\nGoing to delete: "<< name << endl;
delete []name;
}
if(nationality){
delete []nationality;
cout << "Going to nationality: "<< nationality << endl;
}
}
citizen(const citizen &obj){
int size = sizeof(obj.name);
name = new char[size+1];
strcpy(name, obj.name);
int size2 = sizeof(obj.nationality);
nationality = new char[size2 + 1];
strcpy(nationality, obj.nationality);
cout << "\nI'm copy construcotr\n";
}
};
答案 0 :(得分:0)
终止的原因是第一行,它应该是
#include <iostream>
(注意:没有.h
)。旧标题iostream.h
已被弃用(实际上它从未正式成为C ++的一部分)。
然后会出现更多错误,例如使用char*
代替const char*
,当您需要帮助解决问题时,请询问。