C++模板哈希表put方法编译错误(C2923)求助
C++哈希表模板编译错误修复
核心错误原因
编译错误Error C2923 'HashTable<int,std::string>::HashItem': 'key' is not a valid template type argument for parameter 'Key',是因为在put方法中错误地使用了函数参数key和value作为HashItem的模板参数——模板参数需要的是类型,而key、value是具体的变量实例,应该使用类模板定义的Key、Value(大写)类型。
全面修正步骤
1. 移除内部类HashItem的重复模板声明
HashItem作为HashTable的嵌套类,已经可以直接访问外部类的Key、Value模板参数,无需再单独声明模板:
// 原错误代码 template<typename Key, typename Value> class HashItem { ... }; // 修正后 class HashItem { ... };
2. 修正put方法中的HashItem模板参数
将HashItem<key, value>全部替换为HashItem(嵌套类已绑定外部模板类型),或显式写HashItem<Key, Value>:
// 原错误代码 table = new list<HashItem<key, value>>[CAPACITY]; HashItem<key, value>* hi = new HashItem(key, value); // 修正后 table = new list<HashItem>[CAPACITY]; HashItem hi(key, value); // 直接在栈上创建对象,避免内存泄漏
3. 修复Value成员的初始化问题
Value类型为std::string时,不能用nullptr初始化(nullptr是指针类型),移除= nullptr:
// 原错误代码 Value value = nullptr; // 修正后 Value value;
4. 初始化table指针避免野指针
在构造函数中初始化table为nullptr,防止未分配内存时访问野指针:
HashTable() : table(nullptr) { cout << "ht cstructed, intitial_capacity is (default:) " << CAPACITY << endl; } HashTable(int initial_capacity) : table(nullptr), CAPACITY(initial_capacity) { cout << "ht cstructed, intitial_capacity is " << initial_capacity << endl; }
5. 补充缺失的头文件
std::find需要包含<algorithm>头文件,std::hash需要包含<functional>头文件,否则会编译错误:
#include <algorithm> #include <functional>
6. 修正哈希函数的模板兼容性
当前hash_function仅重载了int和string,无法适配任意Key类型,改用模板化的哈希函数:
int hash_function(const Key& key) { return std::hash<Key>{}(key) % CAPACITY; }
7. 修正put方法的计数逻辑
原代码不管元素是否插入成功都递增current_total,导致计数错误,仅在插入成功时递增:
if (find(table[hash].begin(), table[hash].end(), hi) == table[hash].end()) { table[hash].push_back(hi); current_total++; // 仅插入成功时计数 }
修正后的完整代码
HashTable.h
#pragma once #include <cmath> #include <iostream> #include <list> #include <algorithm> #include <functional> using namespace std; template<typename Key, typename Value> class HashTable { class HashItem { public: Key key; Value value; bool operator==(const HashItem& hi) const { return key == hi.key && value == hi.value; } HashItem(const Key& k, const Value& v) : key(k), value(v) {} HashItem& operator=(const Value& v) { this->value = v; return *this; } operator string() { return this->value; } }; list<HashItem>* table = nullptr; int current_total = 0; const float FILL_LEVEL = 0.8; int CAPACITY = 100; const int PRIME_CONST = 31; int hash_function(const Key& key) { return std::hash<Key>{}(key) % CAPACITY; } void rehash() { // 补充rehash实现逻辑 } public: HashTable() : table(nullptr) { cout << "ht cstructed, intitial_capacity is (default:) " << CAPACITY << endl; } HashTable(int initial_capacity) : table(nullptr), CAPACITY(initial_capacity) { cout << "ht cstructed, intitial_capacity is " << initial_capacity << endl; } void put(Key key, Value value) { if (table == nullptr) table = new list<HashItem>[CAPACITY]; HashItem hi(key, value); int hash = hash_function(key); if (find(table[hash].begin(), table[hash].end(), hi) == table[hash].end()) { table[hash].push_back(hi); current_total++; } if (current_total > (FILL_LEVEL * CAPACITY)) { rehash(); } } };
main.cpp
#include <iostream> #include "HashTable.h" using namespace std; #define DEBUG(X) cout << (#X) << " = " << (X) << endl HashTable<int, string> ht; void put_test() { cout << "--------------- put test ----------------------------------" << endl; ht.put(10, "test"); } int main() { put_test(); return 0; }
内容的提问来源于stack exchange,提问作者marie_antoinette
相关产品推荐
相关产品推荐

