C ++使用模板类LinkedList需要模板参数

时间:2013-10-19 18:54:10

标签: c++ templates linked-list nodes

因此,当我的Linked List类仅使用ints而不是模板T时,它工作得很好,但是当我通过并将所有内容更改为templatized时,它给了我并且错误地说“使用模板类LinkedList需要模板参数” 。

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include <iostream>
#include <memory>

template<typename T>
class LinkedList
{
private:

    struct Node
    {
        T data;
        std::shared_ptr<Node> next;

        Node(T d, std::shared_ptr<Node> n)
        :data(d)
        ,next(n)
        {}
        Node()
        {};

        T getData()
        {
            return data;
        }

    };


    std::shared_ptr<Node> head;
    std::shared_ptr<Node> current;


public:


    LinkedList()
    :head()
    {}
    LinkedList(LinkedList& other)
    :head(Clone(other.head))
    {}

^This is where I think the problem is but did I just forget to replace an int somewhere?  I've looked this over a lot and I haven't found anything but I could just be blind.  

    std::shared_ptr<Node> getStart() const
    {
        return head;
    }

    //Insertion
    void InsertAt(T value, std::shared_ptr<Node> &n)
    {
        n = std::make_shared<Node>(value, n);

    }

    void Insertion(T value)
    {
        Insertion(value, head);
    }

    void Insertion(T value, std::shared_ptr<Node> &n)
    {
        if (!n)
        {
            InsertAt(value, n);
            return;
        }

        if (value > n->data)
            Insertion(value, n->next);
        else
            InsertAt(value, n);

    }

    //Deletion
    void Remove(T value)
    {
        Remove(value, head);
    }

    void Remove(T value, std::shared_ptr<Node>& n)
    {
        if (!n) return;
        if (n->data == value)
        {
            n = n->next;
            Remove(value, n);
        }
        else
        {
            Remove(value, n->next);
        }
    }



    void for_each(const std::shared_ptr<Node> &n)
    {
        if(!n) return;

        else
        {
            std::cout<<n->data<<std::endl;
            for_each(n->next);
        }

    }



    std::shared_ptr<Node> Clone(std::shared_ptr<Node> n) const
    {
        if(!n) return nullptr;
        return std::make_shared<Node>(n->data, Clone(n->next));
    }

    LinkedList& operator = (const LinkedList &list)
    {
        head = list.head;
        this->Clone(head);
        return *this;
    }



};



#endif

1 个答案:

答案 0 :(得分:0)

您是否也更改了使用LinkedList的代码?

例如,整数的链接列表:LinkedList<int> list;