下面是代码:链接错误 - 新模板
#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 &)" ([email protected]?
[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@@AAE?AV?
[email protected][email protected][email protected][email protected]@[email protected]@V?
[email protected]@[email protected]@[email protected]@@@@[email protected]@@[email protected][email protected]@[email protected]@V?
[email protected]@[email protected]@[email protected]@@Z)
fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.
我是相当新的模板使用,并没有正式地学习它,而是黑客通过网络而不是阅读一章。我在哪里出错了这个代码,并且必须修改哪些代码才能生成最专业的代码?
你有在同一文件中的一切吗? –