c++ - InsertNode function shutting down program -
i having trouble coding linked list template. compiles these errors:
1>main.obj : error lnk2019: unresolved external symbol "public: __thiscall linkedlist::~linkedlist(void)" (??1?$linkedlist@h@@qae@xz) referenced in function _main
1>main.obj : error lnk2019: unresolved external symbol "public: void __thiscall linkedlist::deletenode(int)" (?deletenode@?$linkedlist@h@@qaexh@z) referenced in function _main
1>main.obj : error lnk2019: unresolved external symbol "public: void __thiscall linkedlist::displaylist(void)" (?displaylist@?$linkedlist@h@@qaexxz) referenced in function _main
1>main.obj : error lnk2019: unresolved external symbol "public: void __thiscall linkedlist::insertnode(int)" (?insertnode@?$linkedlist@h@@qaexh@z) referenced in function _main
1>c:\users\pr0hz\documents\visual studio 2010\projects\listtemplate\debug\listtemplate.exe : fatal error lnk1120: 4 unresolved externals
any appreciate have been staring @ screen past couple of hours trying solve problem. here code:
main.cpp
#include <iostream> #include <conio.h> #include "linkedlist.h" using namespace std; int main() { linkedlist<int> list; list.insertnode(5); list.insertnode(7); list.insertnode(1); list.insertnode(19); list.insertnode(16); list.insertnode(22); cout << "\ndisplay list: \n"; list.displaylist(); cout << endl; list.deletenode(16); cout << "\ndisplay node 16 deleted: \n"; list.displaylist(); cout << endl; linkedlist<int> list2 = list; cout << "\ndisplay list 2: \n"; list2.displaylist(); cout << endl; getch(); return 0; }
linkedlist.h
#ifndef linkedlist_h #define linkedlist_h #include <iostream> using namespace std; template <class t> class linkedlist { private: struct listtemplate { t value; listtemplate *next; }; listtemplate *head; public: //constructor linkedlist() { head = null; } //destructor ~linkedlist(); //copy constructor linkedlist(listtemplate &obj) { listtemplate *nodeptr = &obj; //move through list listtemplate *newnode = new listtemplate; if(!head) return; else { newnode = head; while(nodeptr->next) { nodeptr = nodeptr->next; newnode->next = nodeptr; } } } void appendnode(t); void insertnode(t); void deletenode(t); void displaylist(); }; #endif
linkedlist.cpp
#include "linkedlist.h" #include <iostream> using namespace std; template <class t> void linkedlist<t>::appendnode(t newvalue) { listtemplate *newnode; //point new node listtemplate *nodeptr; //move through list //allocate new node , store num there newnode = new listtemplate; newnode->value = num; newnode->next - null; if(!head) head = newnode; else { //initialize nodeptr head move through list nodeptr = head; //while there still node, move through until there no more while (nodeptr->next) nodeptr = nodeptr->next; //append node nodeptr->next = newnode; } } template <class t> void linkedlist<t>::insertnode(t newvalue) { listtemplate *newnode; //new node listtemplate *nodeptr; //move through list listtemplate *previousnode = null; //the previous node //allocate new node , store num newnode = new listtemplate; newnode->value = num; //if no node, new node head if (!head) { head = newnode; newnode->next = null; } else //insert node needs { nodeptr = head; //beginning position previousnode = null; //skip nodes less newnode while(nodeptr != null && nodeptr->value < num) { previousnode = nodeptr; nodeptr = nodeptr->next; } //if newnode 1st in list, insert in slot 1 if (previousnode == null) { head = newnode; newnode->next = nodeptr; } else //insert after previous node { previousnode->next = newnode; newnode->next = nodeptr; } } } template <class t> void linkedlist<t>::deletenode(t searchnum) { listtemplate *nodeptr; listtemplate *previousnode; //if list empty, nothing if(!head) return; //see if first node 1 if(head->value == num) { nodeptr = head->next; delete head; head = nodeptr; } else //find node delete { nodeptr = head; //skip nodes not equal num while (nodeptr != null && nodeptr->value != num) { previousnode = nodeptr; nodeptr = nodeptr->next; } //delete node , link previous node 1 of 1 being deleted if (nodeptr) { previousnode->next = nodeptr->next; delete nodeptr; } } } template <class t> linkedlist<t>::~linkedlist() { listtemplate *nodeptr; listtemplate *nextnode; nodeptr = head; while (nodeptr != null) { nextnode = nodeptr->next; delete nodeptr; nodeptr = nextnode; } } template <class t> void linkedlist<t>::displaylist() { listtemplate *nodeptr; nodeptr = head; while(nodeptr) { cout << nodeptr->value << " "; nodeptr = nodeptr->next; } }
once again, help!
you shouldn't implementing template functions in source files.
see here - why can templates implemented in header file?
try moving template functions header file (linkedlist.h), , recompile
Comments
Post a Comment