Thread: LNK2019 and LNK1120 Errors?

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164

    LNK2019 and LNK1120 Errors?

    Hello,
    I have downloaded source code from the textbook website and have run into these linker errors:

    T3Q21_List_Back error LNK2019: 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 orderedLinkedListType<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@ std@@AAV01@ABV?$orderedLinkedListType@H@@@Z) referenced in function _main

    T3Q21_List_Back fatal error LNK1120: 1 unresolved externals

    I have been able to narrow the source of the problem to the ostream operator overloading function because when I remark out the cout statements that output the class object the program compiles fine. But, I don't know what is wrong with the code because every program in this chapter has the same problem. The overload function is below and the main function is below that. I am running VS2003 as my IDE. Any insight to my problem would help me a bunch.
    Code:
    //Overloading the stream insertion operator
    template<class Type>
    ostream& operator<<(ostream& osObject, const linkedListType<Type>& list)
    {
    	nodeType<Type> *current; //pointer to traverse the list
    	current = list.first;                //set current so that it points to 
                                                                //the first node
    	while(current != NULL)         //while more data to print
    	{
    	   osObject << current->info << " ";
    	   current = current->link;
    	}
    
    	return osObject;
    }
    Code:
    //Program to test various operations on an ordered linked list
    #include <iostream> 
    #include "orderedLinkedList.h"
    using namespace std;
    int main()
    {
         orderedLinkedListType<int> list1, list2;              //Line 1
         int num;                                                              //Line 2
         cout<<"Line 3: Enter integers ending with -999"
               <<endl;                                                         //Line 3
         cin>>num;                                                          //Line 4
         while(num != -999)                                             //Line 5
         {
    		list1.insertNode(num);             //Line 6
    		cin>>num;                               //Line 7
         }
         cout<<endl;                                                        //Line 8
         cout<<"Line 9: List 1: "<<list1<<endl;               //Line 9
         list2 = list1; //test the assignment operator; Line 10
         cout<<"Line 11: List 2: "<<list2<<endl;             //Line 11
         cout<<"Line 12: Enter the number to be "
                <<"deleted: ";			//Line 12
         cin>>num;                                                          //Line 13
         cout<<endl;                                                        //Line 14
         list2.deleteNode(num);                                       //Line 15
         cout<<"Line 16: After deleting the node, "
               <<"List 2: "<<endl<<list2
               <<endl;                                                         //Line 16
         return 0;					
    }

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I don't see an #include statement in your main .cpp that's including the header where your operator<<() is defined.

    Also, you need to include <ostream> in the header with your operator<<() as well as your linked list header.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    I probably didn't include enough information. I tried to keep it short and lost some things.
    LinkedList header file include statements:
    Code:
    #ifndef H_LinkedListType
    #define H_LinkedListType
    
    #include <iostream>
    #include <cassert>
    using namespace std;
    
    template <class Type>
    struct nodeType
    {
    	Type info;
    	nodeType<Type> *link;
    };
    
    template<class Type>
    class linkedListType
    {
    	friend ostream& operator<<(ostream&, const linkedListType<Type>&);
    orderedLinkedList header file include statements:
    Code:
    #ifndef H_orderedLinkedListType
    #define H_orderedLinkedListType
    
    #include <iostream>
    #include <cassert>
    
    #include "linkedList.h"
    
    using namespace std;
    
    template<class Type>
    class orderedLinkedListType: public linkedListType<Type>
    {
    So, is everything there that needs to be?
    Last edited by clegs; 11-21-2007 at 11:04 PM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Templated classes NEED to be in a headerfile that is included in your source (.C, .cpp) file. It won't do to declare them in another .C/.cpp file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    The operator << is declared as a friend and the definition is written in the linked list header file. The ordered link list class is derived from linked list and contains the include statement for linked list. The .cpp file is just the test file for these two linked list classes. That is all there is to the project. two header files and the cpp test file.

    I still can't figure out what is wrong with the operator << overload function that keeps it from compiling.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    using namespace std;
    This is unrelated to your problem, but you should NEVER have a using statement in a header file. When you start writing larger programs, it could bite you in the ass with namespace conflicts...

  7. #7
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Thanks for the heads up on the "using" statement in the header file, unfortunately this code is directly from the source code of the textbook I am using for this class. I can't get it to compile as is, which is necessary before I can write my functions in the doublyLinkedList class for my programming homework assignment. The problem isn't just with this project, it is with every project code included for this chapter. I have posts all over - to the instructor, the university student board and here. So far this board is the only response I have received, but the problem hasn't been identified yet. I am still checking with additional references I have, and haven't found the problem yet either.

    Thanks for all the input so far. As I said, these are the only responses I've received so far.

    The code is at:

    http://www.course.com/studentcenter/...TOKEN=51810148

    And the code problem is in Chapter 5. I think it all has to do with the overloaded << operator function in the linkedListType class which is inherited by the doublyLinkedList class and used in the testProgLinkedList.cpp test file.
    Last edited by clegs; 11-22-2007 at 01:36 PM.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    The code is incorrect.

    Here's a (handy) explanation and solution.

    https://forums.microsoft.com/MSDN/Sh...60571&SiteID=1

    gg

  9. #9
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Codeplug:

    Thank you SO MUCH!

    It worked!!!

    I fought this for two days and got nowhere, before I can actually write my assignment.

    Now, after cooking the dinner I can actually progress with my homework code. So, I may be back if I run into a snag there.

    Thanks again,
    and HAPPY THANKSGIVING!
    Last edited by clegs; 11-22-2007 at 01:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Linker Errors
    By beene in forum Game Programming
    Replies: 6
    Last Post: 11-18-2006, 12:09 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Visual C++
    By gvector1 in forum Windows Programming
    Replies: 20
    Last Post: 03-14-2003, 11:37 AM
  5. A bunch of compiler errors
    By Dimeslime99 in forum Game Programming
    Replies: 4
    Last Post: 07-14-2002, 11:01 PM