0

我一直在研究双向链表代码,每次尝试编译时都无法找到导致错误的原因。抛出的错误是

main.obj:错误 LNK2019:函数 >_main 1>Doubly List.exe 中引用的未解析外部符号“public:__thiscall DoublyList::DoublyList(void)”(??0?$DoublyList@H@@QAE@XZ):致命错误 LNK1120:1 未解决的外部

DoublyList.h -> http://pastebin.com/5wbeKksv
DoublyListNode.h 和 main.cpp -> http://pastebin.com/vVdGpgaW

4

4 回答 4

2

您声明但未定义DoublyList默认构造函数。它的析构函数也是如此。

于 2011-10-26T17:47:11.687 回答
0

与您的问题无关,我知道这不是代码审查部分,但这是我的一些想法。

在您的插入功能中

DoublyListNode < T > *newPtr = new DoublyListNode< T >(tempData);
newPtr->nextPtr = newPtr->prePtr = NULL;
if(newPtr == NULL)
{
     cout << "Insert cannot allocate memory\n";
} //end if

应该

DoublyListNode < T > *newPtr = new DoublyListNode< T >(tempData);
if(newPtr == NULL)
{
     cout << "Insert cannot allocate memory\n";
}else{
     newPtr->nextPtr = newPtr->prePtr = NULL;
     // rest of code

另外,在您的查找功能中

DoublyListNode< T > *currentPtr = head;
for(int i = 1; i < index; i++)
{
     currentPtr = currentPtr->nextPtr;
} // end for

应该

DoublyListNode< T > *currentPtr = head;
for(int i = 1; currentPtr && (i < index); i++)
{
     currentPtr = currentPtr->nextPtr;
} // end for

此外,由于您使用的是 C++,请考虑使您的索引基于 0(查看您的代码,它们是基于 atm 1 的)

于 2011-10-26T18:44:26.530 回答
0

您已经为 DoublyList 声明了一个构造函数,但没有定义它。在您的 DoublyList() 之后添加一个 {} 并查看它是否有效,取下 ; 也。

于 2011-10-26T17:47:31.950 回答
0

您已经定义了复制构造函数,但忘记定义默认构造函数:

template< class T >
DoublyList< T > :: DoublyList() : head(NULL), size( 0 )
{
    // empty body
} // end DoublyList
于 2011-10-26T17:51:36.657 回答