【问题标题】:use of class template requires template argument list and no appropriate default constructor使用类模板需要模板参数列表并且没有适当的默认构造函数
【发布时间】:2017-03-20 08:41:04
【问题描述】:

我不确定我在哪里做错了。我正在尝试将字符串名称和密钥从类包装传递给类 hashT

C2955 :'hashT': 使用类模板需要模板参数列表

C2512 : 'hashT' : 没有合适的默认构造函数可用

template<class T>
class hashT{
public:
    hashT(const int newKey, const string newName= "") :theList(newKey,newName){
         key = newKey;
         name = newName;
         theList.push_back(key);
         theList.push_back(name);
    }

    hashT & operator=(const hash &rhs) {
          if (this != &rhs) {
          key = rhs.getKey(); 
          name = rhs.getName();
          desc = rhs.getDes();
       }
       return *this;
   }


private:
   vector<T>theList;
   int key;
   string name; 
}

类包装

class wrap{
  public:
    wrap(){  myList = new hashT[1000];

    ~wrap(){ delete[]myList;}

    //getter
     hashT<class T> *getHashing() const{
        return mylist;
      }

     //setter
     void setNextHash(hashT<class T> *hashelement){
        mylist = hashelement;
      }

  private:
        hashT<class T> *mylist;
 }

【问题讨论】:

    标签: c++ class templates hashtable


    【解决方案1】:

    您的班级wrap 有多个错误。 hashT&lt;class T&gt;* 这不是您将模板定义为返回类型的方式。

    你必须写:

    template<typename T>
    hashT<T> *getHashing() const {
        return mylist;
    }
    

    setNextHash 也一样。

    但是你不能创建模板变量,所以你只有一种定义mylist的方法,让wrap成为一个模板类,就像hashT一样。

    【讨论】:

    • 而且不能声明像hashT&lt;class T&gt; *mylist;这样的成员
    • @Danh Jup 忘记了抱歉 :) 谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多