错误c2440&#39; =&#39;无法从int *转换为Type <t> *

时间:2015-08-26 05:54:52

标签: c++ templates

我在VS2015中收到以下错误。 对于我搞乱模板的内容,我并不明白。

非常感谢任何指针!

错误C2440&#39; =&#39;:无法转换为&#39; int *&#39;到&#39; DNode *&#39;

    template<class Type>
    class DNode <- *** THIS IS THE TYPE ***
    {
    public:
        Type *next;
        Type *previous;
        Type value;

        DNode(Type valueParam)
        {
            value = valueParam;
            next = previous = NULL;
        }
    };

    template<class T>
    class DLinkedList
    {
        DNode<T> *head;
        DNode<T> *tail;

    public:
        DLinkedList()
        {
            head = tail = NULL;
        }

        T pop_tail()
        {
            if (tail == NULL) return -1;
            T value;
            if (head == tail)
            {
                value = tail->value;
                free(tail);
                head = tail = NULL;
                return value;
            }
            else
            {
                DNode<T> *ptr = tail;
                value = tail->value;
                tail = tail->previous;   <-- *** THIS LINE THROWS ERR ***
                tail->next = NULL;
                free(ptr);
                return value;
            }
        }
    }

1 个答案:

答案 0 :(得分:4)

DNode::previous的类型为Type*,而不是DNode<Type>*

您可能希望将DNode::nextDNode::previous声明为DNode<Type>*类型。