簡體   English   中英

雙鏈表 - 導致我的代碼拋出編譯錯誤的原因是什么?如何解決?

[英]Doubly Linked List - What is causing my code to throw a compiler error and how can I fix it?

我一直在研究Doubly Linked List代碼,而且每次嘗試編譯時我都無法找到導致錯誤的原因。 拋出的錯誤是

main.obj:錯誤LNK2019:未解析的外部符號“public:__thiscall DoublyList :: DoublyList(void)”(?? 0?$ DoublyList @ H @@ QAE @ XZ)在函數> _main 1> Doubly List.exe中引用:致命錯誤LNK1120:1個未解析的外部

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

您聲明但不定義DoublyList默認構造函數。 它的析構函數也是如此。

您已為DoublyList聲明了構造函數但未定義它。 在你的DoublyList()之后添加一個{}並查看它是否有效取消了; 太。

您已經定義了復制構造函數,但忘記定義默認構造函數:

template< class T >
DoublyList< T > :: DoublyList() : head(NULL), size( 0 )
{
    // empty body
} // end DoublyList

與你的問題無關,我知道這不是代碼審查部分,但這里有一些我的想法。

在你的插入功能

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開始(查看您的代碼,它們是基於1的)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM