我决定创建一个列表(出于练习的原因,所以请不要忘记重新发明轮子谢谢..)
我得到了这个 LNK2019 错误,我相信它的代码是特定的,因为我似乎找不到修复它的方法(是的,我在这里搜索)
我发现问题是因为编译器不承认方法的定义,但我似乎无法找出原因。我正在使用 MVS 2010,因此链接参数是自动完成的。这是我的代码。
清单 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;
}