鱼C论坛

 找回密码
 立即注册
查看: 1851|回复: 4

c++ 类模版问题

[复制链接]
发表于 2021-10-29 16:06:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<iostream>
  2. using namespace std;
  3. // pair 自带的容器
  4. template<class K,class E>
  5. class hashTable {
  6.   public:
  7.     hashTable(int theDiv=13,float theMaxFactor=0.8);
  8.     int search(const K& theKey) const;
  9.     pair<K,E>* find(const K& theKey) const;
  10.     void insert(const pair<K,E>& thePair);
  11.     int hash(const K& theKey);//用此函数代替
  12.     void changeTable();//2*size+1 超过定义因子


  13.    private:
  14.      pair<K,E>** table;
  15.      int dSize;
  16.      int divisor;//也是默认桶的数量
  17.      float MaxFactor;//用户指定
  18.      //hash<K> hash;
  19. };
  20. template<class K,class E>
  21. hashTable<class K,class E>::hashTable(int theDiv,float theMaxFactor){
  22.          divisor=theDiv;
  23.          MaxFactor=theMaxFactor;

  24.          table= new pair<K,E>* [divisor];
  25.         for(int i=0;i<divisor;i++)
  26.            table[i]= NULL;     
  27. }
  28. template<class K,class E>
  29. int
  30. hashTable<class K,class E>::hash(const K& theKey){
  31.        //默认就是整形 直接返回
  32.         return theKey;
  33. }
  34. template<class K,class E>
  35. int
  36. hashTable<class K,class E>::search(const K& theKey) const{
  37.   int i= hash(theKey) %divisor;
  38.   int j=i;
  39.   do{
  40.      if(table[j]==NULL ||  table[j]->first== theKey)
  41.        return j;
  42.       j=(j+1)%divisor;
  43.   }while(j!=i);

  44.     return j;
  45. }
  46. template<class K,class E>
  47. std::pair<K,E>* //要加命名空间
  48. hashTable<class K,class E>::find(const K& theKey) const{
  49. int b=search(theKey);
  50.    if(table[b]==NULL || table[b]->first != theKey)
  51.      return NULL;

  52.      return table[b];
  53. }

  54. template<class K,class E>
  55. void
  56. hashTable<class K,class E>::insert(const std::pair<K,E>& thePair){
  57.    int b=search(thePair.first);

  58.    if(table[b]==NULL) {
  59.       table[b] = new pair<K,E>(thePair);
  60.       dSize++;
  61.    }
  62.     else {
  63.       if( table[b]->first == thePair.first;)
  64.           table[b]->second= thePair.second;
  65.             else
  66.               cout<<"FULL!"<<endl;
  67.     }
  68.        if((float)dSize/divisor>MaxFactor)
  69.          changeTable();
  70.       
  71. }
  72. template<class K,class E>
  73. void
  74. hashTable<class K,class E>::changeTable(){//copy
  75.      pair<K,E>** temp = pair<K,E> new[dSize*2+1];
  76.      copy(table,table+dSize,temp);
  77.       divisor= = dSize*2+1;
  78.       delete table;
  79.       table = temp;
  80. }


  81. int main(){
  82.    

  83.     return 0;
  84.       
  85. }
复制代码


报错说(Template parameter list matching the non-templated nested type 'hashTable<K, E>' should be empty ('template<>')
怎么解决呢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-10-29 16:06:50 | 显示全部楼层
是哪里出了问题呀 不太懂c++
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-10-29 16:40:14 | 显示全部楼层
报错你应该先翻译一下啊  它说的是你模板参数列表应该匹配非模板嵌套类型
你类名后面不用加上模板类型  直接用类名试试啊
  1. template<class K,class E>
  2. void hashTable::changeTable(){//copy
  3.      pair<K,E>** temp = pair<K,E> new[dSize*2+1];
  4.      copy(table,table+dSize,temp);
  5.       divisor= = dSize*2+1;
  6.       delete table;
  7.       table = temp;
  8. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-29 18:04:46 | 显示全部楼层
yuxijian2020 发表于 2021-10-29 16:40
报错你应该先翻译一下啊  它说的是你模板参数列表应该匹配非模板嵌套类型
你类名后面不用加上模板类型  直 ...

[错误]在没有参数列表的情况下,模板名称“hashTable”的使用无效
删了还是没用
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-29 19:31:16 | 显示全部楼层
哦哦 解决了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-16 16:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表