我决定创建一个列表(出于练习原因,所以请关闭重新启动方向盘,谢谢..)
我得到了这个LNK2019错误,我相信它的代码特定因为我似乎无法找到解决方法(是的,我在这里提供)
我发现问题是错误的,编制者不承认方法的定义,但我似乎无法发现whay。 我正在使用MVS 2010,因此链接args会自动完成。这是我的代码。
列表H:
#ifndef _DLIST
#define _DLIST
#include "Node.h"
template<class DataO>
class Dlist
{
private:
/* Data Members */
Node<DataO>* _ndHead;
Node<DataO>* _ndTail;
unsigned int _nLength;
/* Methods */
void Delete();
public:
/* Ctors && Dtors */
Dlist();
~Dlist();
/* Getters && Setters */
int Length();
/* Methods */
void Add(DataO doData);
void Clear();
void RemoveAt(unsigned int nIndex);
void Remove();
};
#endif
列出Cpp:
// Dlist.cpp
#include "Dlist.h"
/* ------------------ Dlist Section ------------------ */
/* --- Constracturs && Destractur --- */
template<class DataO>
Dlist<DataO> :: Dlist()
{
// Init data
this->_ndHead = NULL;
this->_ndTail = NULL;
this->_nLength = 0;
}
template<class DataO>
Dlist<DataO> :: ~Dlist()
{
// Check if we have nodes to delete
if (this->_nLength > 0);
{
this->Clear();
}
}
/* --- Methods -- */
template<class DataO>
int Dlist<DataO> :: Length()
{
// Return the length
return (this->Length);
}
template<class DataO>
void Dlist<DataO> :: Add(DataO doData)
{
// Check if head is null
if (this->_ndHead == NULL)
{
// Create head
this->_ndHead = new Node<DataO>(doData, NULL, NULL);
}
// Check if tail is null
else if (this->_ndTail == NULL)
{
// Create tail
this->_ndTail = new Node<DataO>(doData, NULL, this->_ndHead);
// Set head's next
this->_ndHead->SetNext(this->_ndTail);
}
// Create a new Node and replace the tail.
else
{
// Create a new node.
this->_ndTail->_ndNext = new Node<DataO>(doData, NULL, this->_ndTail);
// Make the new node the tail
this->_ndTail = this->_ndTail->_ndNext;
}
}
节点H:
template<class DataO>
class Node
{
private:
/* Data Members */
DataO _doData;
Node<DataO>* _ndPrev;
Node<DataO>* _ndNext;
/* Methods */
void Delete();
public:
/* Ctors && Dtors */
Node<DataO>(DataO doData);
Node<DataO>(DataO doData, const Node& ndNext, const Node& ndPrev);
~Node();
/* Getters && Setters */
DataO& GetData();
void SetData(DataO doData);
const Node& GetNext();
void SetNext(const Node* ndNextNode);
const Node& GetPrev();
void SetPrev(const Node* ndPrevNode);
};
节点Cpp:
// Node.cpp
#include "Node.h"
/* ------------------ Node Section ------------------ */
/* --- Constracturs --- */
template<class DataO>
Node<DataO> :: Node(DataO doData)
{
// Init data
this->SetData(doData);
this->SetNext(NULL);
this->SetPrev(NULL);
}
template<class DataO>
Node<DataO> :: Node(DataO doData, const Node& ndNext, const Node& ndPrev)
{
// Init data
this->SetData(doData);
this->SetNext(ndNext);
this->SetPrev(ndPrev);
}
/*
- Node destructor -
- Maneges the change of nodes.
- Changes the Previous node next node,
- to the current node next node.
*/
template<class DataO>
Node<DataO> :: ~Node()
{
// Check that prev is not null
if (this->GetPrev != NULL)
{
// Set prevs Next to current next
this->GetPrev().SetNext(this->GetNext());
}
}
/*
- GetData -
- Returns the data stored in the Node.
*/
template<class DataO>
DataO& Node<DataO> :: GetData()
{
// Return the data
return (this->_doData&);
}
/*
- SetData -
- Sets the data stored in the Node.
*/
template<class DataO>
void Node<DataO> :: SetData(DataO doData)
{
// Sets data
this->_doData = doData;
}
/*
- GetNext -
- Returns the next node in the list.
*/
template<class DataO>
const Node<DataO>& Node<DataO> :: GetNext()
{
// Returns the next node in the list.
return (this->_ndNext&);
}
/*
- SetNext -
- Set the next node in the list.
*/
template<class DataO>
void Node<DataO> :: SetNext(const Node* ndNextNode)
{
// Set next node as given node
this->_ndNext = ndNextNode;
}
/*
- GetPrev -
- Returns the previous node in the list.
*/
template<class DataO>
const Node<DataO>& Node<DataO> :: GetPrev()
{
// Returns the next node in the list.
return (this->_ndPrev&);
}
/*
- SetPrev -
- Set the previous node in the list.
*/
template<class DataO>
void Node<DataO> :: SetPrev(const Node* ndPrevNode)
{
// Set next node as given node
this->GetPrev = ndPrevNode;
}
答案 0 :(得分:1)
这是因为MSVC编译器不支持extern模板。我看到你的想法,这将是正确的方法,但它不适用于大多数编译器。这是因为编译器需要访问整个模板定义(而不仅仅是签名)才能为模板的每个实例化生成代码。您有2个选项可以使其工作:
定义头文件中的所有方法(与大多数Boost库一样)
明确地为预定义类型实例化您的模板类(这样您的类将不是通用的)
查看详细解释here