| | |
compile error
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2005
Posts: 38
Reputation:
Solved Threads: 0
The assignment I am working on is to create a doubly linked list. This is what I have thus far. However when I compile I get 2 errors: LNK2001 unresolved external symbol and LNK1120 unresolved externals. Was hoping someone could look over my code and help me with these errors.
C++ Syntax (Toggle Plain Text)
#ifndef H_doublyLinkedList #define H_doublyLinkedList #include <iostream> #include <cassert> using namespace std; template <class Type> struct nodeType { Type info; nodeType<Type> *next; nodeType<Type> *back; }; template <class Type> class doublyLinkedList { friend ostream& operator<<(ostream&, const doublyLinkedList<Type>&); public: const doublyLinkedList<Type>& operator= (const doublyLinkedList<Type> &); void initializeList(); bool isEmptyList(); void destroy(); void reversePrint(); int length(); Type front(); Type back(); bool search(const Type& searchItem); void insertNode(const Type& insertItem); void deleteNode(const Type& deleteItem); doublyLinkedList(); doublyLinkedList(const doublyLinkedList<Type>& otherList); ~doublyLinkedList(); protected: int count; nodeType<Type> *first; nodeType<Type> *last; private: void copyList(const doublyLinkedList<Type>& otherList); }; template<class Type> doublyLinkedList<Type>::doublyLinkedList() { first= NULL; last = NULL; count = 0; } template<class Type> bool doublyLinkedList<Type>::isEmptyList() { return(first == NULL); } template<class Type> void doublyLinkedList<Type>::destroy() { nodeType<Type> *temp; while(first != NULL) { temp = first; first = first->next; delete temp; } last = NULL; count = 0; } template<class Type> void doublyLinkedList<Type>::initializeList() { destroy(); } template<class Type> int doublyLinkedList<Type>::length() { return count; } template<class Type> ostream& operator<<(ostream& osObject, const doublyLinkedList<Type>& list) { nodeType<Type> *current; current = list.first; while(current != NULL) { cout<<current->info<<" "; current = current->next; } return osObject; } template<class Type> void doublyLinkedList<Type>::reversePrint() { nodeType<Type> *current; current = last; while(current != NULL) { cout<<current->info<<" "; current = current->back; } } template<class Type> bool doublyLinkedList<Type>::search(const Type& searchItem) { bool found; nodeType<Type> *current; found = false; current = first; while(current != NULL && !found) if(current->info >= searchItem) found = true; else current = current->next; if(found) found = (current->info == searchItem); return found; } template<class Type> Type doublyLinkedList<Type>::front() { assert(first != NULL); return first->info; } template<class Type> Type doublyLinkedList<Type>::back() { assert(last != NULL); return last->info; } template<class Type> void doublyLinkedList<Type>::insertNode(const Type& insertItem) { nodeType<Type> *current; nodeType<Type> *trailCurrent; nodeType<Type> *newNode; bool found; newNode = new nodeType<Type>; assert(newNode != NULL); newNode->info = insertItem; newNode->next = NULL; newNode->back = NULL; if(first == NULL) { first = newNode; last = newNode; count++; } else { found = false; current = first; while(current != NULL && !found) if(current->info >= insertItem) found = true; else { trailCurrent = current; current = current->next; } if(current == first) { first->back = newNode; newNode->next = first; first = newNode; count++; } else { if(current != NULL) { trailCurrent->next = newNode; newNode->back = trailCurrent; newNode->next = current; current->back = newNode; } else { trailCurrent->next = newNode; newNode->back = trailCurrent; last = newNode; } count++; } } } template<class Type> void doublyLinkedList<Type>::deleteNode(const Type& deleteItem) { nodeType<Type> *current; nodeType<Type> *trailCurrent; bool found; if(first == NULL) cerr<<"Cannot delete from an empty list"<<endl; else if(first->info == deleteItem) { current = first; first = first->next; if(first != NULL) first->back = NULL; else last = NULL; count--; delete current; } else { found = false; current = first; while(current != NULL && !found) if(current->info >= deleteItem) found = true; else current = current->next; if(current == NULL) cout<<"The item to be deleted is not in the list" <<endl; else if(current->info == deleteItem) { trailCurrent = current->back; trailCurrent->next = current->next; if(current->next != NULL) current->next->back = trailCurrent; if(current == last) last = trailCurrent; count--; delete current; } else cout<<"The item to be deleted is not in list." <<endl; } } template<class Type> void doublyLinkedList<Type>::copyList(const doublyLinkedList<Type>& otherList) { nodeType<Type> *newNode; nodeType<Type> *current; if(first != NULL) destroy(); if(otherList.first == NULL) { first = NULL; last = NULL; count = 0; } else { current = otherList.first; count = otherList.count; first = new nodeType<Type>; assert(first != NULL); first->info = current->info; first->next = NULL; last = first; current = current->next; while(current != NULL) { newNode = new nodeType<Type>; assert(newNode!= NULL); newNode->info = current->info; newNode->next = NULL; last->next = newNode; last = newNode; current = current->next; } } } template<class Type> doublyLinkedList<Type>::doublyLinkedList(const doublyLinkedList<Type>& otherList) { first = NULL; copyList(otherList); } template<class Type> const doublyLinkedList<Type>& doublyLinkedList<Type>::operator= (const doublyLinkedList<Type> & otherList) { if(this != &otherList) copyList(otherList); return *this; } template<class Type> doublyLinkedList<Type>::~doublyLinkedList() { destroy(); } #endif #include <iostream> #include "doublyLinkedList.h" using namespace std; int main() { doublyLinkedList<int> list1, list2; int num; cout << "Enter integers ending with -999" << endl; cin >> num; while(num != -999) { list1.insertNode(num); cin>>num; } cout << endl; cout << "List 1: " << list1 << endl; list2 = list1; cout << "List 2: " << list2 << endl; cout << "Enter the number to be deleted: "; cin >> num; cout << endl; list2.deleteNode(num); cout << "After deleting the node, " << "List 2: " << endl << list2 << endl; return 0; }
•
•
Join Date: Oct 2005
Posts: 38
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Ancient Dragon
what function(s) did the compiler say was missing? Look at your code and see if you coded them. If they are in a library somewhare did you include the library?
Well I ran the code, and it compiled and linked just fine. Since you have pasted codes for two files, doublyLinkedList.h and main.cpp in the same code window, I only had to seperate them and compile. Otherthan that I didnt do any changes. THe program ran fine, I entered some integers upto -999 and two lists appeared on the screen. However I got a runtime error in the
function. Apparently you are accessing an invalid pointer. If you have coded all this in a single file, ( which I dont think you are, but just in case ), break it up and see. If you get a linker error even then, my guess is you have not included a necessary library, but without the missing function name I cant say specifically what.
C++ Syntax (Toggle Plain Text)
void doublyLinkedList<Type>::deleteNode(const Type& deleteItem)
•
•
Join Date: Oct 2005
Posts: 38
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Ancient Dragon
read those error messages again. The compiler (really the linker) is saying that one or more functions are missing. And it tells you the name of them.
C++ Syntax (Toggle Plain Text)
doublyLinkedList error LNK2001: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class doublyLinkedList<int> const &)" (??6@$$FYAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$doublyLinkedList@H@@@Z)
•
•
Join Date: Oct 2005
Posts: 38
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Ancient Dragon
Your program compiles and links without error using VC++ 6.0 compiler, just like WolfPack said it would. I tried the same thing with Visual Studio 2003 and got the same error that you did, but unfortunately I havn't been able to figure out the problem either.
The functions I wrote are "copyList", constructor with parameters, = operator overload, and the destructor. Could you verify that those functions are correct? As the error might be originating from one of those functions.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Great :mrgreen: coding the friend function at the time it was declared solved the problem.
C++ Syntax (Toggle Plain Text)
template <class Type> class doublyLinkedList { friend ostream& operator<<(ostream& os, const doublyLinkedList<Type>& list) { nodeType<Type> *current; current = list.first; while(current != NULL) { os<<current->info<<" "; current = current->next; } return os; } // rest of class here
![]() |
Similar Threads
- visual c++ strange compile error... (C++)
- compile error (C++)
- C++ Compile Error's (C++)
- Out of Error Compile error (Visual Basic 4 / 5 / 6)
- compile error (C)
- PLEASE help with project - compile error (C++)
Other Threads in the C++ Forum
- Previous Thread: Default values for STL container function arguments?
- Next Thread: Can anybody tell what's wrong with this code?
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






