我收到错误代码C2753 - ' HashTable:部分专业化无法匹配主要模板的参数列表 - 当我尝试构建项目时。
我的HashTable.h文件:
#pragma once //should prevent error C2953..... 'HashTable': class template has already been defined. https://stackoverflow.com/questions/10157416/error-class-template-has-already-been-defined
#include <string>
#include <iostream>
#include <iomanip>
#include "Record.h"
using namespace std;
/*Create a closed hash HashTable using Double Hashing & Templates and does not allow duplicates.*/
template <class T>
class HashTable<T> {
private:
int numItems;
int hashSize;
Record<T> *hashArray;
int hashOne(int) const;
int hashTwo(int) const;
public:
/*TEMPLATE*/
HashTable();
~HashTable();
bool insert(int, T, int&);
bool remove(int);
bool find(int, T&);
float alpha();
int getNumItems();
int getSize();
bool print();
/*Source: Partial Code for Overriding print method from StackOverflow.com*/
ostream& print(ostream& os) const {
os << "-----------------------------------------------------------------------" << endl;
os << "Hash Table Slot #|" << setw(20) << "Key and Value" << setw(12) << "" << endl;
os << "-----------------------------------------------------------------------" << endl;
for (int i = 0; i < hashSize; i++)
{
os << setw(10) << i << setw(8) << "|";
if (hashArray[i].isEmpty())
os << setw(16) << hashArray[i] << setw(14) << "" << endl;
else if (hashArray[i].isTombstone())
os << setw(20) << hashArray[i] << setw(12) << "" << endl;
else
os << setw(12) << hashArray[i] << endl;
}
os << "-----------------------------------------------------------------------" << endl;
return os;
}
}; //End of HashTable Class
/*Constructor of the HashTable.
Initializes the size to 1000 and numItems to 0.
Loop to fill hashArray with empty records.*/
template <class T>
HashTable<T>::HashTable() {
this->hashSize = 1000;
this->numItems = 0;
hashArray = new Record<T>[hashSize];
for (int i = 0; i < hashSize; i++) {
hashArray[i] = Record<T>();
}
} //end of Constructor
/*Deconstructor for HashTable. Deletes hashArray.*/
template <class T>
HashTable<T>::~HashTable() {
delete[] hashArray;
} //end of Deconstructor
/*Insert a new key/value pair into the HashTable, duplicates not allowed.
If added return true, return the numver of collisions in int& collisions,and increment numItems.
Otherwise return false.*/
template<class T>
bool HashTable<T>::insert(int key, T value, int& collisions) {
if (numItems >= hashSize) {
cout << "HashArray FULL!" << endl;
return false;
}
if (hashArray[hashOne(key)].isNormal()) {
int loc = 0;
if (hashArray[(hashOne(key) + loc) % hashSize].getKey() == key) {
return false;
}
while (hashArray[(hashOne(key) + loc) % hashSize].isNormal()) {
collisions++;
loc = loc + hashTwo(key);
if (hashArray[(hashOne(key) + loc) % hashSize].getKey() == key) {
return false;
}
}
hashArray[(hashOne(key) + loc) % hashSize] = Record<T>(key, value);
numItems++;
return true;
}
else {
hashArray[hashOne(key)] = Record<T>(key, value);
numItems++;
return true;
}
return false;
} //end insert method
//...other methods go here...//
//hashOne & hashTwo functions are my last two functions, if that matters.
我的.cpp文件实际上只是一块骨头,但这里是:
#include "stdafx.h"
#include "HashTable.h"
int main() {
return 0;
}
我正在使用Visual Studio 2015&amp;已经看过Here,here,here和here试图找到解决方案,以及来自Google和cplusplus.com的众多其他来源,但这些都没有解决了我的问题。
非常感谢任何帮助或建议!
答案 0 :(得分:0)
template <class T>
class HashTable<T> {
应该是
template <class T>
class HashTable {
在定义主类模板时,只有在定义专业化时,才能在类名后面加上参数列表。