鱼C论坛

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

c++ 类模版问题

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

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

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

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


   private:
     pair<K,E>** table;
     int dSize;
     int divisor;//也是默认桶的数量
     float MaxFactor;//用户指定
     //hash<K> hash;
};
template<class K,class E>
hashTable<class K,class E>::hashTable(int theDiv,float theMaxFactor){
         divisor=theDiv;
         MaxFactor=theMaxFactor;

         table= new pair<K,E>* [divisor];
        for(int i=0;i<divisor;i++)
           table[i]= NULL;     
}
template<class K,class E>
int
hashTable<class K,class E>::hash(const K& theKey){
       //默认就是整形 直接返回 
        return theKey;
}
template<class K,class E>
int
hashTable<class K,class E>::search(const K& theKey) const{
  int i= hash(theKey) %divisor;
  int j=i;
  do{
     if(table[j]==NULL ||  table[j]->first== theKey)
       return j;
      j=(j+1)%divisor;
  }while(j!=i);

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

     return table[b];
}

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

   if(table[b]==NULL) {
      table[b] = new pair<K,E>(thePair);
      dSize++;
   }
    else {
      if( table[b]->first == thePair.first;)
          table[b]->second= thePair.second;
            else 
              cout<<"FULL!"<<endl;
    }
       if((float)dSize/divisor>MaxFactor)
         changeTable();
      
}
template<class K,class E>
void
hashTable<class K,class E>::changeTable(){//copy
     pair<K,E>** temp = pair<K,E> new[dSize*2+1];
     copy(table,table+dSize,temp);
      divisor= = dSize*2+1;
      delete table;
      table = temp;
}


int main(){
    

    return 0;
      
}

报错说(Template parameter list matching the non-templated nested type 'hashTable<K, E>' should be empty ('template<>')
怎么解决呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-10-29 16:06:50 | 显示全部楼层
是哪里出了问题呀 不太懂c++
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

使用道具 举报

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

[错误]在没有参数列表的情况下,模板名称“hashTable”的使用无效
删了还是没用
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-10-29 19:31:16 | 显示全部楼层
哦哦 解决了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-7-4 14:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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