为什么我的重载=抛出错误的分配?

时间:2016-02-29 05:59:55

标签: c++ overloading bad-alloc

这是我的功能。

    template<class KEY, class T, int (*thash)(const KEY& a)>
HashMap<KEY, T, thash>& HashMap<KEY, T, thash>::operator =(const HashMap<KEY, T, thash>& rhs) {
      if (this == &rhs)
        return *this;

      delete_hash_table(map, bins);
      bins = rhs.bins, used = rhs.used;
      hash = rhs.hash;
      map = this->copy_hash_table(rhs.map, rhs.bins);
      std::cout << *this << std::endl;
      ++mod_count;
      std::cout << "DONEZO" << std::endl;
      return *this;
}

它调用的唯一其他实际函数是 delete_hash_table ,它不会抛出分配错误(因为它正在解除分配),而 copy_hash_table ,我认为可能会抛出错误,但我的std :: cout打印就好了。

以防万一,这是我的copy_hash_table函数。

    template<class KEY, class T, int (*thash)(const KEY& a)>
typename HashMap<KEY, T, thash>::LN* HashMap<KEY, T, thash>::copy_list(LN* l) const {
    if (l == nullptr)
        return nullptr;
    return new LN(Entry(l->value.first, l->value.second), copy_list(l->next));
}

template<class KEY, class T, int (*thash)(const KEY& a)>
typename HashMap<KEY, T, thash>::LN** HashMap<KEY, T, thash>::copy_hash_table(LN** ht, int bins) const {
    std::cout << "ASSIGNMENT" << std::endl;
    LN **to_return = new LN*[bins];
    for (int x = 0; x < bins; x++)
        to_return[x] = copy_list(ht[x]);
    return to_return;
}
在我回来之前,

Donezo 会被打印出来。我不确定是什么导致错误。

错误: std :: bad_alloc在测试体中抛出。

0 个答案:

没有答案