在哈希表复制构造函数中找不到错误
Can't find bug in hash table copy constructor
当使用我的复制构造函数执行哈希表的副本时,例如:
LPTable<int> hashtableCopy = hashtable;
程序崩溃,我不确定为什么。我已经通过调试器,它似乎知道它在 for 循环中接收什么值,所以我对正在发生的事情感到困惑,是语法/逻辑问题还是什么。任何帮助将不胜感激,谢谢。我将发布到目前为止我所尝试的内容。
具有复制 CTOR 的哈希表
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_;
}
你
从不在复制构造函数中设置LargerMax
、max_
或size_
。 (您也不会在构造函数中设置max_
int
。
复制 other.records_
时,不会为所需的记录分配空间。 如果other.records_
中的指针是nullptr
,只需将其设置为 records_[i]
中的新指针值,否则将其设置为使用 Records 复制构造函数分配的新记录。 将while
循环替换为
records_[i] = other.records_[i] == nullptr ? nullptr : new Record(other.records_[i])
相关文章:
- 我在c ++矩阵计算器程序中找不到错误
- 我在代码中找不到错误以找到最大的素因数?
- 在我的应用逻辑中找不到错误
- 在模板类中找不到错误
- 双重释放或损坏错误(找不到错误?
- 在我的 cpp 或头文件中找不到错误,类构建失败
- 在哈希表复制构造函数中找不到错误
- 找不到错误。C++,字符串
- 8谜题代码.我的队列没有填充,我找不到错误的位置
- C++中的预期表达式,但找不到错误
- C++ 将布尔隐式转换为 int 和算术,删除分支,找不到错误
- 找不到错误"invalid conversion from char to const char*"的解决方案
- 在以下代码中找不到错误:- 在 try() 之前预期未限定 id
- 在重载运算符中找不到错误<
- C++ 中的分段错误(核心转储) 找不到错误
- 在使用set_difference算法时找不到错误
- 在 while 循环中找不到错误?
- C++错误LNK2001,找不到错误
- 在我的代码中找不到错误....分段错误
- LNK2005(已定义)找不到错误