나는 이러한 변수를 가지고 :C++ 복사 생성자/할당 연산자 오류
char** wordList_;
int wordListCapacity_;
int* wordCountList_;
char* fileName_;
int nUniqueWords_;
int nTotalWords_;
int nTotalCharacters_;
내 복사 생성자 :
FileIndex::FileIndex(const FileIndex& fi)
{
fileName_ = new char[strlen(fi.fileName_) + 1];
strcpy(fileName_, fi.fileName_);
cout << "Jiasd?" << endl;
wordListCapacity_ = fi.wordListCapacity_;
nUniqueWords_ = fi.nUniqueWords_;
nTotalWords_ = fi.nTotalWords_;
nTotalCharacters_ = fi.nTotalCharacters_;
wordList_ = new char*[wordListCapacity_];
wordCountList_ = new int[wordListCapacity_];
for(int i = 0; i < nUniqueWords_; i++) {
wordList_[i] = fi.wordList_[i];
wordCountList_[i] = fi.wordCountList_[i];
}
}
내 과부하 할당 연산자 : 나는의 FileIndex을 만들 때마다
FileIndex& FileIndex::operator=(const FileIndex& fi)
{
fileName_ = new char[strlen(fi.fileName_) + 1];
strcpy(fileName_, fi.fileName_);
wordListCapacity_ = fi.wordListCapacity_;
nUniqueWords_ = fi.nUniqueWords_;
nTotalWords_ = fi.nUniqueWords_;
nTotalCharacters_ = fi.nTotalCharacters_;
wordList_ = new char*[wordListCapacity_];
wordCountList_ = new int[wordListCapacity_];
for (int i = 0; i < nUniqueWords_; i++) {
wordList_[i] = new char[strlen(fi.wordList_[i])+1];
strcpy(wordList_[i], fi.wordList_[i]);
wordCountList_[i] = fi.wordCountList_[i];
}
return *this;
}
(FirstIndex
) 초기화하고 무언가를 사용하여 멤버 변수를 초기화하십시오. gful (하지 NULL) 나는 복사 생성자와 대입 연산자 테스트하는이 라인이 있습니다
FileIndex secondIndex = firstIndex;
FileIndex thirdIndex;
secondIndex = thirdIndex; // Segmentation fault here
나는 할당 연산자와 세그먼트 오류를 얻을 수를하지만 난 그것 때문에 복사 생성자의 결함 코드의 수 있습니다 느낌이있다 . 즉, 복사 생성자에 오류가있는 경우 대입 연산자에도 오류가있을 수 있습니다.
미리 도움을 청하십시오!
수업을 단순화하면 어떻게됩니까? – Beta
소멸자는 어떻게 생겼습니까? –
대신'std :: vector'를 사용하면 문제가 해결 될 것입니다. * ctor-initializer * 목록에 대해서도 배워보십시오. –