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.

#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;					
}

Recommended Answers

All 9 Replies

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?

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?

The compiler did not specify that a function was missing. If I had to guess I would say the error is coming from the copyList function. It looks fine to me but once I added it the errors began.

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.

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

void doublyLinkedList<Type>::deleteNode(const Type& deleteItem)

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.

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.

Here is the error I am getting. As I said I do not see an error about a missing function. But honestly I do not understand how to read the error messages.

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)

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.

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.

I ran the program through my compiler again commenting out lines to see what is causing the error. Turns out the program compiles if I comment out the cout << lines The weird thing is the .h (header file) was supplied to me with the exception of the last four functions. I am really confused now. Could I have written one of the last four functions wrong and that is causing errors in the << operator overload?

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.

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.16

Great :mrgreen: coding the friend function at the time it was declared solved the problem.

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.