그래서 나는 다음과 같은 클래스이상한 this-> 행동
class Community { private: char* Name; char foundationDate[11]; Person* founder; int maxMembersCount; int membersCount; Person* members; static int communitiesCount;
.....
을하고 난 복사 생성자 구현하려는 :
Community::Community(const Community& other)
{
this->Name = new char[strlen(other.Name)+1];
strcpy(this->Name,other.Name);
strcpy(this->foundationDate,other.foundationDate);
this->founder = other.founder;
this->maxMembersCount = other.maxMembersCount;
this->membersCount = other.membersCount;
this->members = new Person[this->maxMembersCount];
this->members = other.members;
communitiesCount++;
}
을 하지만이 코드는 커뮤니티 A = B라고 말할 때마다 충돌합니다. 그래서이 코드는 합법적 인 것처럼 보이지만 디버깅을 시작할 때 메시지가 있습니다.이 "메모리를 읽을 수 없습니다". 더 많은 코드 예제가 필요하면 저에게 알려주십시오. 저에게 알려주세요.
Community::Community(const char* name , char foundDate[],Person* founder,int maxMembers) {
this->Name = new char[strlen(name)+1];
strcpy(this->Name,name);
strcpy(this->foundationDate,foundDate);
this->founder = new Person(founder->getName(),founder->getEGN(),founder->getAddress());
this->maxMembersCount = maxMembers;
this->membersCount = 2;
this->members = new Person[this->maxMembersCount];
communitiesCount++;
}
이 잘 작동 클래스의 기본 생성자입니다 .... 모든
'Name'과'foundationDate'가'null'로 종료되었거나 적절하게 초기화 되었습니까? 또한, 새로운 메모리를'this-> memebers'에 할당하고 포인터를 바로 덮어 씁니다, 그러나 나는 그것이 당신이보고있는 문제를 일으키는 것으로 생각하지 않습니다. –