这是代码:
#ifndef LINKED_LIST_H_
#define LINKED_LIST_H_
#include<memory>
template <class T>
class LinkedList{
public:
//
LinkedList() {}
LinkedList(const T &data);
LinkedList(const T &data, LinkedList &node);
LinkedList(const LinkedList &object);
LinkedList &operator=(const LinkedList &object);
~LinkedList() {}
std::shared_ptr<LinkedList> push_back(const T& data);
private:
T data;
std::unique_ptr<LinkedList> link;
std::unique_ptr<LinkedList> LinkFactory(const LinkedList &node);
std::shared_ptr<LinkedList> CreateStartNode(const T &data);
std::shared_ptr<LinkedList> CreateNode(const T &data, const LinkedList &node);
};
//
template <typename T>
LinkedList<T>::LinkedList(const T &data) : data(data), link(nullptr) {}
template <typename T>
LinkedList<T>::LinkedList(const T &data, LinkedList &node) : data(data) { this->link =
LinkFactory(node); }
template <typename T>
LinkedList<T>::LinkedList(const LinkedList<T> &object) : data(object.data)
link(object.link) {}
template <typename T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T> &object){
data = object.data;
link = object.link;
return *this;
}
template <typename T>
std::unique_ptr<LinkedList<T>> LinkFactory(const LinkedList<T> &node){
return std::unique_ptr<LinkedList<T>>(new LinkedList(node))
}
//
template <typename T>
std::shared_ptr<LinkedList<T>> LinkedList<T>::CreateStartNode(const T &data){
return std::shared_ptr<LinkedList<T>>(new LinkedList(data));
}
//
template <typename T>
std::shared_ptr<LinkedList<T>> CreateNode(const T &data, const LinkedList<T> &node){
return std::shared_ptr<LinkedList<T>>(new LinkedList(data, node));
}
template <typename T>
std::shared_ptr<LinkedList<T>> LinkedList<T>::push_back(const T &data){
//
if(this == 0){
// create new node
return CreateStartNode(data);
}else{
// add a node
return CreateNode(data, *this);
}
}
#endif
这是链接错误:
error LNK2001: unresolved external symbol "private: class std::tr1::shared_ptr<class
LinkedList<class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > > > __thiscall LinkedList<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > >::CreateNode(class
std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
const &,class LinkedList<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> > > const &)" (?CreateNode@?
$LinkedList@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@AAE?AV?
$shared_ptr@V?$LinkedList@V?$basic_string@DU?$char_traits@D@std@@V?
$allocator@D@2@@std@@@@@tr1@std@@ABV?$basic_string@DU?$char_traits@D@std@@V?
$allocator@D@2@@4@ABV1@@Z)
fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
我对模板的使用相当陌生,并没有正式学习它,而是通过网络 hack-n-slash 而不是阅读一章。这段代码我哪里出了问题,必须进行哪些更正才能生成最专业的代码?