首页 > 解决方案 > 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;
} 

标签: c++linked-list

解决方案


请注意,您的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>*地方。insertLastconst KeyValue<K, V>&

如果您在new此处删除关键字,它应该可以作为一个新对象创建,然后在insertLast函数内复制。


推荐阅读