以下是代码:
#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.
我是模板使用的新手,并没有正式学习它,而是通过网络进行黑客攻击而不是阅读章节。我在哪里弄错了这段代码,必须做出哪些更正才能生成最专业的代码?
答案 0 :(得分:1)
您对CreateNode函数的定义不在LinkedList范围内。
此致
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>::CreateNode(const T &data, const LinkedList<T> &node)
{
return std::shared_ptr<LinkedList<T>>(new LinkedList(data, node));
}
答案 1 :(得分:1)
在类定义之外定义成员函数时,需要使用LinkedList<T>::
限定成员名称以表明它是成员;否则,它会声明一个新的非成员函数。
您忘记了对CreateNode
和LinkFactory
:
template <typename T>
std::unique_ptr<LinkedList<T>> LinkedList<T>::LinkFactory(const LinkedList<T> &node){
// Add this ^^^^^^^^^^^^^^^
return std::unique_ptr<LinkedList<T>>(new LinkedList(node));
}