使用类模板需要模板参数列表,有什么用?

时间:2014-12-12 09:38:47

标签: c++ templates

我有这个我正在创建的模板类

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;}

按照目前的编写,我收到错误&#34;错误C2955:&#39; ArrayLLN&#39;:使用类模板需要模板参数列表&#34;

以下配置产生了其他错误

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; 
 }

2 个答案:

答案 0 :(得分:2)

正确的语法是这样的:

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

答案 1 :(得分:1)

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

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