下面是代碼:鏈接錯誤 - 新模板
#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.
我是相當新的模板使用,並沒有正式地學習它,而是黑客通過網絡而不是閱讀一章。我在哪裏出錯了這個代碼,並且必須修改哪些代碼才能生成最專業的代碼?
你有在同一文件中的一切嗎? –