0

因此,当我的 Linked List 类仅使用 int 而不是模板 T 时,它工作得很好,但是当我经历并更改要模板化的所有内容时,它给了我和错误说“使用模板类 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
4

1 回答 1

0

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

例如整数链表:LinkedList<int> list;

于 2013-10-20T01:34:29.750 回答