因此,当我的链表类仅使用整数而不是模板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
您是否还更改了使用LinkedList
的代码?
例如,ints的链表:LinkedList<int> list;