【问题标题】:use of class template requires template argument list, what do?使用类模板需要模板参数列表,怎么办?
【发布时间】:2014-12-12 09:38:47
【问题描述】:

我正在创建这个模板类

template <class T> class ArrayLLN {
     private:
     ArrayLLN *next;
     T *item;
public:
     ArrayLLN(T i, ArrayLLN *n);
     ~ArrayLLN();
     void insert(T n, int i, int m);
     ArrayLLN *getnext();
     T *getitem();
     T remove(int i, int m);
};

我遇到问题的方法如下。

ArrayLLN *getnext();

并写成

template <class T> ArrayLLN ArrayLLN <T> :: *getnext(){return next;}

正如目前所写,我收到错误“错误 C2955: 'ArrayLLN': 使用类模板需要模板参数列表”

以下配置产生了其他错误

template <class T> ArrayLLN *ArrayLLN <T> :: getnext(){return next;)

如何解决这个错误?

可能是这些声明?

template <class T> T *ArrayLLN<T>  :: getitem(){return item;}
template <class T> ArrayLLN<T> *ArrayLLN<T>::getnext() { return next; }
template <class T> T ArrayLLN <T> :: remove (int i, int m){
    T *tmp == NULL;
    if(i == m && next){
        tmp = item;
        item = next ->getitem();
        next = next->getnext();
    }
    else if (i > m){
        m++;
        tmp = next -> remove(i,m);
    }
    return tmp; 
 }

【问题讨论】:

  • “以下配置产生了其他错误” - 还有哪些错误?
  • 我没有收到与您的新代码相同的错误。 T *tmp == NULL 应该是 T *tmp = NULL

标签: c++ templates


【解决方案1】:

正确的语法是这样的:

                        //  +-- argument list here                    +-- bracket
                        //  v                                         v not paren
template <class T> ArrayLLN<T> *ArrayLLN<T>::getnext() { return next; }

【讨论】:

  • 不幸的是,这仍然给我同样的错误。
  • 奇数。能否提供MCVE 以便我重现错误?
  • 我提供了其余的方法声明,并在我原来的帖子中添加了其他位。到目前为止,这就是我为这门课得到的所有代码。我对模板真的很陌生,所以我不知道该怎么做。编辑:我想我不太确定如何为此提供更少的代码,因为我觉得我的代码量非常少。
  • 我能看到的唯一(编译时)错误是 T *tmp == NULL; 必须是 T *tmp = NULL 并且 remove 必须返回一个指针才能按照它的编写方式工作。我的编译器毫无怨言地吃掉了剩下的。
  • 所以我发现错误实际上来自不同的文件。我今晚显然很累。这显然是提供错误的原因。模板 类 ArrayLL { 私有:ArrayLLN *head; int size;} 我来确定改变 ArrayLLN *head;到 ArrayLLN
【解决方案2】:

在类内部,ArrayLLN 是一个注入的类名,允许您省略模板参数列表。在课堂之外,您必须提供。其次,明星在错误的地方。

template <class T> ArrayLLN<T>* ArrayLLN <T> :: getnext(){return next;}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多