2017-03-31 41 views
0

錯誤:找不到在哈希表的拷貝構造函數

LPTable<int> hashtableCopy = hashtable; 

程序崩潰,而且我不確定爲什麼。我經歷了調試器,它似乎知道它在for循環中接收到的值,所以我對發生了什麼,如果它是語法/邏輯問題還是什麼感到困惑。任何幫助將不勝感激,謝謝。我會發布我迄今爲止嘗試過的。

與複製哈希表構造函數

template <class TYPE> 
class LPTable :public Table<TYPE> { 

    struct Record { 
      TYPE data_; 
      string key_; 
      bool isDeleted = false; 

      Record() { 
        key_ = ""; 
        data_ = 0; 
        isDeleted = false; 
      } 

      Record(const string& key, const TYPE& data) { 
        key_ = key; 
        data_ = data; 
        isDeleted = false; 
      } 

    }; 

    Record** records_; //the table 
    int LargerMax;  // *1.35 max_ 
    int max_;   //capacity of the array 
    int size_;   //current number of records held 
    int MyHash(string key); // custom hash function 
    int numRecords() const { return this.size_; } 
    bool isEmpty()   { return size_ = 0; } 

public: 
    LPTable(int maxExpected); 
    LPTable(const LPTable& other); 
    LPTable(LPTable&& other); 
    virtual bool update(const string& key, const TYPE& value); 
    virtual bool remove(const string& key); 
    virtual bool find(const string& key, TYPE& value); 
    virtual const LPTable& operator=(const LPTable& other); 
    virtual const LPTable& operator=(LPTable&& other); 
    virtual ~LPTable(); 
}; 
/* none of the code in the function definitions below are correct. You can replace what you need 
*/ 
template <class TYPE> 
LPTable<TYPE>::LPTable(int maxExpected) : Table<TYPE>() { 
    LargerMax = maxExpected * 1.35; 

    records_ = new Record*[LargerMax]; 

    for (int i = 0; i < LargerMax; i++) 
    { 
      records_[i] = nullptr; 
    } 

    size_ = 0; 
} 

//copy ctor 
template <class TYPE> 
LPTable<TYPE>::LPTable(const LPTable<TYPE>& other) { 


    records_ = new Record*[other.LargerMax];  

    for (int i = 0; i < other.LargerMax; i++) 
    { 
      while (other.records_[i] != nullptr) 
      { 
        records_[i]->key_ = other.records_[i]->key_; 
        records_[i]->data_ = other.records_[i]->data_; 
        records_[i]->isDeleted = other.records_[i]->isDeleted; 
      } 
    } 

} 

template <class TYPE> 
const LPTable<TYPE>& LPTable<TYPE>::operator=(const LPTable<TYPE>& other) 
{ 

    LPTable temp(other); 
    std::swap(temp.records_, records_); 
    std::swap(temp.max_, max_); 
    std::swap(temp.size_, size_); 
    return *this; 

} 


template <class TYPE> 
const LPTable<TYPE>& LPTable<TYPE>::operator=(LPTable<TYPE>&& other) { 
    return *this; 

} 
template <class TYPE> 
LPTable<TYPE>::~LPTable() { 

    delete[] records_; 
} 
+0

更換while環',而(other.records_ [I] = nullptr!)' - 你不必在這個循環改變'other.records_',因此它可能是無限 –

+0

@Alexander Lapenkov我也嘗試了一個for循環,並給了我同樣的崩潰 – user7795742

+0

我認爲這可能是因爲初始化時默認的ctor不知所措 – user7795742

回答

0

你永遠不會在你的拷貝構造函數設置LargerMaxmax_,或size_。 (在構造函數中,您不要設置max_,要麼是int)。

當您複製other.records_時,您沒有爲需要的記錄分配空間。如果other.records_中的指針爲nullptr,只需將其設置爲records_[i]中的新指針值,否則將其設置爲使用記錄拷貝構造函數分配的新記錄。與

records_[i] = other.records_[i] == nullptr ? nullptr : new Record(other.records_[i])