万千只cnm 发表于 2021-10-29 16:06:15

c++ 类模版问题

#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>* ;
      for(int i=0;i<divisor;i++)
         table= 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==NULL ||table->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==NULL || table->first != theKey)
   return NULL;

   return table;
}

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==NULL) {
      table = new pair<K,E>(thePair);
      dSize++;
   }
    else {
      if( table->first == thePair.first;)
          table->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;
   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<>')
怎么解决呢{:5_100:}

万千只cnm 发表于 2021-10-29 16:06:50

是哪里出了问题呀 不太懂c++

yuxijian2020 发表于 2021-10-29 16:40:14

报错你应该先翻译一下啊它说的是你模板参数列表应该匹配非模板嵌套类型
你类名后面不用加上模板类型直接用类名试试啊
template<class K,class E>
void hashTable::changeTable(){//copy
   pair<K,E>** temp = pair<K,E> new;
   copy(table,table+dSize,temp);
      divisor= = dSize*2+1;
      delete table;
      table = temp;
}

万千只cnm 发表于 2021-10-29 18:04:46

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

[错误]在没有参数列表的情况下,模板名称“hashTable”的使用无效
删了还是没用{:5_104:}

万千只cnm 发表于 2021-10-29 19:31:16

哦哦 解决了
页: [1]
查看完整版本: c++ 类模版问题