2016-11-25 5 views
-1

私はHashTableを詳細にコピーしようとしています。私のハッシュテーブルは、リンクされたリストでいっぱいの配列です。 私は既にリンクリストコンストラクタ/コピーコンストラクタ/オーバーライド演算子をコーディングしており、完全に動作します。ディープコピー(おそらくポインタ関連)

次のようにコード化しましたが、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'という名前のメンバ変数と、同じ名前のコピーコンストラクタの***ローカル変数***を持つことが問題になるとは思わないでしょうか? –

+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; 
}; 

第二ステップからDefaultCapacityパラメータを使用しますHashTable(void);デフォルトコンストラクタ。

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]); 
    } 
} 
関連する問題