简体   繁体   English

双链表 - 导致我的代码抛出编译错误的原因是什么?如何解决?

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

I've been working on a Doubly Linked List code and I'm unable to locate what is causing an error every time I attempt compiling. 我一直在研究Doubly Linked List代码,而且每次尝试编译时我都无法找到导致错误的原因。 The error being thrown is 抛出的错误是

main.obj : error LNK2019: unresolved external symbol "public: __thiscall DoublyList::DoublyList(void)" (??0?$DoublyList@H@@QAE@XZ) referenced in function >_main 1>Doubly List.exe : fatal error LNK1120: 1 unresolved externals 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 DoublyList.h - > http://pastebin.com/5wbeKksv
DoublyListNode.h and main.cpp -> http://pastebin.com/vVdGpgaW DoublyListNode.h和main.cpp - > http://pastebin.com/vVdGpgaW

You declare but don't define DoublyList default constructor. 您声明但不定义DoublyList默认构造函数。 Same goes for its destructor. 它的析构函数也是如此。

You have declared a constructor for DoublyList but not defined it. 您已为DoublyList声明了构造函数但未定义它。 Add a {} after your DoublyList() and see if it works take off the ; 在你的DoublyList()之后添加一个{}并查看它是否有效取消了; too. 太。

You've defined the copy constructor, but forgotten to define the default constructor: 您已经定义了复制构造函数,但忘记定义默认构造函数:

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

Not related to your question, and I know this isn't the codereview section, but here are some of my thoughts. 与你的问题无关,我知道这不是代码审查部分,但这里有一些我的想法。

In your insert function 在你的插入功能

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

should be 应该

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

Also, in your find function 此外,在您的查找功能

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

should be 应该

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

Also, since you are using C++, consider making your indexes 0-based (looking at your code they are atm 1-based) 此外,由于您使用的是C ++,请考虑使索引从0开始(查看您的代码,它们是基于1的)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM