2016-11-25 3 views
-1

그래서 해시 테이블을 딥 복사하려고합니다. 내 해시 테이블은 연결된 목록으로 채워진 배열입니다. 링크 된 목록 생성자/복사 생성자/재정의 연산자를 이미 코딩했으며 완벽하게 작동합니다.딥 복사 (아마도 포인터 관련)

그래서 다음 코드를 코딩했지만 for 루프에 문제가 있습니다. 호출 된() 중단 :

HashTable.cpp 
HashTable::HashTable() 
{ 

} 
HashTable::HashTable(const HashTable & ht) 
{ 
    bucketSize = ht.bucketSize; 
    count = ht.count; 
    LinkedList** table = new LinkedList*[ht.bucketSize]; 

    for (int i = 0; i < bucketSize; i++) { 
     table[i] = new LinkedList(ht.table[i]); 
    } 

} 

HashTable.h 
class HashTable { 
public: 
    HashTable(); 
    HashTable(const HashTable& ht); 
private: 
    // Add your member variables and any private member functions here 
    int bucketSize = defaultCapacity; //default is 101 
    int count = 0; 
    LinkedList table[defaultCapacity]; 
} 

나는 그러나 내가 실행 누를 때 오류 메시지 만 팝업이 없다, 그것은 포인터를 함께 할 수있는 뭔가가 생각한다.

+1

만들 [최소, 완전하고, 검증 가능한 예] (http://stackoverflow.com/help/mcve) 우리를 게재하도록 노력하십시오. –

+2

또한, 'table'이라는 이름의 멤버 변수와 동일한 이름을 가진 copy-constructor의 *** 로컬 변수 ***을 가진 것이 문제가 될 것이라고 생각하지 않습니까? –

+0

그 행을 다음과 같이 변경하면 : table = new LinkedList [ht.bucketSize]; 테이블이 수정 가능한 좌변 치 여야한다는 오류가 나타납니다. 나는 그것을 바꾸려고 노력하지 않을 것이다. 새 HashTable을 만들고 기존의 HashTable을 수정하려고합니다. – t3hdaniel

답변

0

제공된 소스 코드에서 클래스 및 생성자 선언 모두에 오류가 있습니다. 이러한 오해를보다 잘 감지하려면 컴파일러에서 구문 분석 할 때 소스 코드를 제시하는 것이 좋습니다.

첫 번째 단계 - class HashTable에서 모든 변수는 선언되고 초기화되지 않습니다.

The table variable has to store an array of LinkedList . It is a LinkedList **table; as declared in the copy-constructor.

class HashTable { 
public: 
    HashTable(void); 
    HashTable(const HashTable& ht); 
private: 
    // Add your member variables and any private member functions here 
    int bucketSize; 
    int count; 
    LinkedList **table; 
}; 

두 번째 단계 - 기본 생성자 HashTable(void);DefaultCapacity 매개 변수를 사용합니다.

In a default constructor HashTable(void) , a defaultCapacity array is created and initialized with default LinkedList() = Empty List.

HashTable::HashTable(void) 
{ 
    bucketSize = defaultCapacity; //default is 101 
    count = 0; 
    table = new LinkedList*[bucketSize]; 
    for (int i = 0; i < bucketSize; i++) { 
     table[i] = new LinkedList(); 
    } 
} 

세 번째 단계 - 복사 생성자 HashTable(const HashTable & ht)는 복제 배열을 만들고 아이템 LinkedList의 중복.

In the copy constructor, each item is initialized with the LinkedList copy constructor.

Error: the local declaration of LinkedList** table override the class declaration.

HashTable::HashTable(const HashTable & ht) 
{ 
    bucketSize = ht.bucketSize; 
    count = ht.count; 
    //LinkedList** table = new LinkedList*[ht.bucketSize]; 
    table = new LinkedList*[ht.bucketSize]; 

    for (int i = 0; i < bucketSize; i++) { 
     table[i] = new LinkedList(ht.table[i]); 
    } 
}