c++ - C ++无法从'KeyValue转换参数1*' 到 'const T &'
问题描述
我正在尝试构建的哈希表存在问题。它应该有一个链接列表数组,每当我调用我为它构建的 insertLast 函数时,我都会收到一个转换错误:
无法将参数 1 从“KeyValue *”转换为“const T &”。
从我在尝试解决这个问题时看到的情况来看,有些东西被错误地声明了,但我无法确定问题出在哪里,或者这是否正确。任何帮助是极大的赞赏。这是一个家庭作业,所以我想请你帮我找出这个问题,如果在这个过程中有任何问题,我宁愿自己找出来。
//hash table class
template <typename K, typename V>
class HashTable
{
friend class LinkedList<KeyValue<K,V>>;
public:
HashTable(const int& bucketCount);
~HashTable();
int buckets;
LinkedList<KeyValue<K, V>> * arr{ nullptr };
void add(const K& key, const V& value);
};// end class HashTable
template<typename K, typename V>
HashTable<K, V>::HashTable(const int& bucketCount) {
buckets = bucketCount;
this->arr = new LinkedList<KeyValue<K, V>>[buckets];
}
template <typename K, typename V>
HashTable<K, V>::~HashTable() {
delete[] arr;
}
template <typename K, typename V>
void HashTable<K, V>::add(const K& key, const V& value) {
//this is the line of code that breaks
arr[std::hash<K>{}(key) % buckets].insertLast(new KeyValue<K, V>(key, value));
}
这是我的具有 insertLast 功能的链表类
//LinkedList class
template <typename T>
class LinkedList
{
public:
~LinkedList();
void insertLast(const T& value);
Iterator<T> begin();
Iterator<T> end();
protected:
Node<T> *front{ nullptr };
Node<T> *back{ nullptr };
int count{ 0 };
};
template <typename T>
LinkedList<T>::~LinkedList() {
if (this->front) {
Node<T> * temp{ this->front->forward };
while (temp) {
delete this->front;
this->front = temp;
temp = temp->forward;
}
delete this->front;
}
}
//this is the function I'm getting an error on
template <typename T>
void LinkedList<T>::insertLast(const T& value) {
Node<T> * temp = new Node<T>();
temp->data = value;
if (!this->front) {
// Specific scenario, list is empty
this->front = temp;
}
else {
// General scenario, at least one node
this->back->forward = temp;
}
this->back = temp;
this->count++;
}
这是我的 KeyValue 类,该表将包含一个类型为 KeyValue 的 LinkedList 数组
//KeyValue class
template <typename K, typename V>
class KeyValue {
public:
K key{};
V value{};
KeyValue();
KeyValue(const K& key, const V& value);
};
template <typename K, typename V>
KeyValue<K, V>::KeyValue() {
}
template <typename K, typename V>
KeyValue<K, V>::KeyValue(const K& key, const V& value) {
this->key = key;
this->value = value;
}
解决方案
请注意,您的LinkedList
变量定义为
LinkedList<KeyValue<K, V>> * arr{ nullptr };
在HashTable
类内部,这意味着这是一个指向LinkedList
包含KeyValues<K, V>
的指针
insertLast
函数定义为:
void insertLast(const T& value);
T
类的模板类型在哪里LinkedList
,即KeyValue<K, V>
这里。
另一方面,您正在尝试将该功能insertLast
用作
arr[std::hash<K>{}(key) % buckets].insertLast(new KeyValue<K, V>(key, value));
这里,new KeyValue<K, V>(key, value)
是 a函数需要 a的KeyValue<K, V>*
地方。insertLast
const KeyValue<K, V>&
如果您在new
此处删除关键字,它应该可以作为一个新对象创建,然后在insertLast
函数内复制。
推荐阅读
- python - how do I make empty lists based on a number of input information?
- r - 使用 r 绘图时如何调整图片的长度和宽度
- python-3.x - 使用 python 进行 Yolo-v3 对象检测
- typescript - 如何将 AWS CDK 合成器输出到终端/网络浏览器 [如果可能]
- python - 自定义删除器方法示例
- c++ - 创建二叉树和搜索函数
- c# - 如何防止防病毒软件检测到我自己的程序与注册表项混淆
- sql - 在单个 JSONB 查询中限制和包含
- plasticscm - 如何增加 Plastic Merge Tool 的可用资源?
- python - NOT NULL 约束失败:snippets_choice.post_id