我正在为人们注册程序,我有重载运算符 = 的问题。
我有类CRegister
、结构Person
和Place
.
struct Places{
char date[11];
char * street;
char * city;
Places & operator = (const Places & other){
delete [] street;
delete [] city;
strncpy (date, other.date, 11);
int len;
len = strlen(other.street);
this->street = new char[len];
strncpy ( this->street, other.street, len );
len = strlen(other.city);
this->city = new char[len];
strncpy ( this->city, other.city, len );
return *this;
}
}
struct Person{
char id[12];
char * name;
char * surname;
Places ** oldPlaces;
int placesCount;
int placesSize;
};
Person & Person::operator =(const Person& other){
for (int i = 0; i < this->placesSize; i++){
delete this->oldPlaces[i];
}
delete [] this->oldPlaces;
delete [] name;
delete [] surname;
placesCount = other.placesCount;
placesSize = other.placesSize;
oldPlaces = new Places*[other.placesSize];
strncpy (id, other.id, 11);
int len;
len = strlen(other.name);
this->name = new char[len];
strncpy ( this->name, other.name, len );
len = strlen(other.surname);
this->surname = new char[len];
strncpy ( this->surname, other.surname, len );
for (int i = 0; i < placesCount; i++){
oldPlaces[i] = other.oldPlaces[i];
}
return *this;
}
class CRegister
{
private:
Person **persons;
int personCount;
int personSize;
};
CRegister& CRegister::operator =(const CRegister& other){
for (int i = 0; i < this->personSize; i++){
delete this->persons[i];
}
delete [] this->persons;
personCount = other.personCount;
personSize = other.personSize;
persons = new Person*[other.personSize];
for (int i = 0; i < personCount; i++){
persons[i] = other.persons[i];
}
return *this;
}
但是,代码已编译,但 Netbeans 显示运行失败。为什么?