title: undefined reference to (function) c++
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/5293021/undefined-reference-to">Undefined Reference to</a>  </p>
  </blockquote>
  
  
  
  <p>I've been banging my head against this one set of error messages for about 4 hours now and I can't seem to figure it out.  I've not posted here before, so I apologize in advance if it's not in the right area or I've done something wrong.  At any rate, the error messages I'm receiving are:</p>
  
  <pre><code>main.cpp|28|undefined reference to `LinkedSortedList&lt;Employee&gt;::LinkedSortedList()'|
     main.cpp|52|undefined reference to `empPrint(LinkedSortedList&lt;Employee&gt;&amp;)'|
     main.cpp|58|undefined reference to `empSave(LinkedSortedList&lt;Employee&gt;&amp;, std::string)'|
     main.cpp|65|undefined reference to `empLoad(LinkedSortedList&lt;Employee&gt;&amp;, std::string)'|
     main.cpp|70|undefined reference to `LinkedSortedList&lt;Employee&gt;::~LinkedSortedList()'|
     main.cpp|70|undefined reference to `LinkedSortedList&lt;Employee&gt;::~LinkedSortedList()'|
  
     obj\Debug\main.o||In function `Z9empSearchR16LinkedSortedListI8EmployeeE':|
     main.cpp|109|undefined reference to `LinkedSortedList&lt;Employee&gt;::getHead()'|
  </code></pre>
  
  <p>My main.cpp is as follows:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;stdio.h&gt;
  #include &lt;fstream&gt;
  #include "SortedList.h"
  #include "LinkedSortedList.h"
  #include "Employee.h"
  #include "LinkedNode.h"
  
  using namespace std;
  
  void newEmp(LinkedSortedList &lt;Employee&gt;&amp; empList);
  void empSearch(LinkedSortedList &lt;Employee&gt;&amp; empList);
  void empPrint(LinkedSortedList &lt;Employee&gt;&amp; empList);
  void empSave(LinkedSortedList &lt;Employee&gt;&amp; empList, string file);
  void empLoad(LinkedSortedList &lt;Employee&gt;&amp; empList, string file);
  
  int main()
  {
      //int empID;
      bool menuFinish = false;
      LinkedSortedList&lt;Employee&gt; empList;
      char selection;
  
      while (!menuFinish)
  
          //simple menu system through cout, the selection is read in through cin
          //and converted to upper case for simplicity during the conditionals
          cout &lt;&lt; "Menu" &lt;&lt; endl;
          cout &lt;&lt; "(I)nsert new record" &lt;&lt; endl;
          cout &lt;&lt; "(E)mployee ID search" &lt;&lt; endl;
          cout &lt;&lt; "(P)rint employee info" &lt;&lt; endl;
          cout &lt;&lt; "(S)ave database to a file" &lt;&lt; endl;
          cout &lt;&lt; "(L)oad database from file" &lt;&lt; endl;
          cout &lt;&lt; "(Q)uit" &lt;&lt; endl;
          cout &lt;&lt; "Enter selection " &lt;&lt; endl;
          cin &gt;&gt; selection;
          selection = toupper(selection);
  
          //menu selections are compared with their functions
          if (selection == 'I')
              newEmp(empList);
          else if (selection == 'E')
              empSearch(empList);
          else if (selection == 'P')
              empPrint(empList);
          else if (selection == 'S')
              {
                  string fileName;
                  cout &lt;&lt; "Enter a filename to save the database to " &lt;&lt; endl;
                  cin &gt;&gt; fileName;
                  empSave(empList, fileName);
              }
          else if (selection == 'L')
              {
                  string fileName;
                  cout &lt;&lt; "Enter the filename to load the database from " &lt;&lt; endl;
                  cin &gt;&gt; fileName;
                  empLoad(empList, fileName);
              }
          else if (selection == 'Q')
              menuFinish = true;
          else
              cout &lt;&lt; "Incorrect choice " &lt;&lt; endl;
  }
  
  //function creates a new employee
  void newEmp(LinkedSortedList &lt;Employee&gt;&amp; empList)
  {
      string firstName;
      string lastName;
      int empID = -1;
  
      cout &lt;&lt; "Please enter the first name " &lt;&lt; endl;
      cin &gt;&gt; firstName;
      cout &lt;&lt; "Please enter the last name " &lt;&lt; endl;
      cin &gt;&gt; lastName;
      while (empID &gt; 9999999 || empID &lt; 0)
          {
          cout &lt;&lt;"Please enter the employee ID " &lt;&lt; endl;
          cin &gt;&gt; empID;
  
          }
      //puts the employee in the db unless they're already found, then outputs an
      //error on the screen
      Employee emp(firstName, lastName, empID);
      bool findEmp = empList.find(emp);
  
      if (!findEmp)
          empList.insert(emp);
      else
          cout &lt;&lt; "Emlpoyee ID " &lt;&lt; empID &lt;&lt; " already in use " &lt;&lt; endl;
  }
  
  //function to search for an employee based on their employee ID
  void empSearch (LinkedSortedList &lt;Employee&gt;&amp; empList)
  {
      int empID;
      int sizeOfList = 0;
      bool noEmp = true;
  
      cout &lt;&lt; "Enter employee ID " &lt;&lt; endl;
      cin &gt;&gt; empID;
  
      LinkedNode &lt;Employee&gt;* temp = empList.getHead();
      while (sizeOfList &lt; empList.size() &amp;&amp; noEmp)
      {
          sizeOfList++;
          if (empID == temp-&gt;value.getEmpID())
              {
                  cout &lt;&lt; "Searched " &lt;&lt; sizeOfList &lt;&lt; "employees " &lt;&lt; endl;
                  cout &lt;&lt; "Found record: " &lt;&lt; temp-&gt;value;
                  noEmp = false;
              }
              temp = temp-&gt;next;
      }
      if (noEmp)
      cout &lt;&lt; "Search of " &lt;&lt; sizeOfList &lt;&lt; " employees.  Employee not found" &lt;&lt; endl;
  
  }
  
  //function used to print the first and last five employees from the db
  void empPrint (LinkedSortedList &lt;Employee&gt;&amp; empList)
  {
      if (empList.size() &lt;= 10)
          {
              empList.print();
          }
  
          else
          {
              LinkedNode&lt;Employee&gt;* temp = empList.getHead();
              cout &lt;&lt; "First five employees: " &lt;&lt; endl;
  
              for (int i = 0; i &lt; 5; i++)
                  {
                  cout &lt;&lt; temp-&gt;value &lt;&lt; endl;
                  i++;
                  temp = temp-&gt;next;
                  }
  
              int midList = empList.size()-5;
              for (int i = 0; i &lt; midList; i++)
                  {
                       temp = temp-&gt;next;
                  }
  
              cout &lt;&lt; "Last five employees: " &lt;&lt; endl;
              for (int i = 0; i &lt; 5; i++)
                  {
                      cout &lt;&lt; temp-&gt;value &lt;&lt; endl;
                      i++;
                      temp = temp-&gt;next;
                  }
          }
  }
  
  //function used to save the employee information from the db to a file
  void empSave(LinkedSortedList&lt;Employee&gt;&amp; empList, string fileName)
  {
      string lastName;
      string firstName;
      //int empID;
      ofstream output;
      output.open(fileName.c_str());
      if (!output)
          {
              cout &lt;&lt; "File not saved" &lt;&lt; endl;
          }
      else
      {
          LinkedNode&lt;Employee&gt;* temp = empList.getHead();
          int i = 0;
          while (i &lt; empList.size())
          {
              output &lt;&lt; temp-&gt;value.getLastName() &lt;&lt; " " &lt;&lt; temp-&gt;value.getFirstName() &lt;&lt; " " &lt;&lt; temp-&gt;value.getEmpID() &lt;&lt; endl;
              i++;
              temp = temp-&gt;next;
          }
      }
      output.close();
  }
  
  //function used to load the employee information from a file to the db
  void empLoad(LinkedSortedList&lt;Employee&gt;&amp; empList, string fileName)
  {
      if (empList.size() &gt; 0)
      {
          empList.clear();
      }
  
      ifstream input;
      input.open(fileName.c_str());
  
      if (!input)
      {
          cout &lt;&lt; "No file exists" &lt;&lt; endl;
      }
  
      else
      {
          int empID;
          string firstName;
          string lastName;
          string delimiter;
  
          while(input.good());
          {
              getline(input, delimiter, '\n');
              getline(input, lastName, ' ');
              getline(input, firstName, ' ');
              input &gt;&gt; empID;
  
              Employee emp(lastName, firstName, empID);
  
              bool empFound = empList.find(emp);
  
              if(!empFound)
              {
                  empList.insert(emp);
              }
              else
              cout &lt;&lt; "Employee already exists" &lt;&lt; endl;
          }
      }
  }
  </code></pre>
  
  <p>My LinkedSortList.cpp is:</p>
  
  <pre><code>#ifndef _LinkedSortedList_
  #define _LinkedSortedList_
  
  #include "Employee.h"
  #include "LinkedSortedList.h"
  #include "SortedList.h"
  #include "LinkedNode.h"
  
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  
  using namespace std;
  
  //default constructor
  //sets head to null and makes the size of the list 0
  template &lt;class Elem&gt;
  LinkedSortedList&lt;Elem&gt;::LinkedSortedList()
  {
      head = NULL;
      listSize = 0;
  }
  
  //destructor, clears list THEN deletes the head so memory leaks are
  //stopped
  template &lt;class Elem&gt;
  LinkedSortedList&lt;Elem&gt;::~LinkedSortedList()
  {
      clear();
      delete head;
  }
  
  //clears the list, freeing memory and stopping leaks and resets the
  //list size
  template &lt;class Elem&gt;
  void LinkedSortedList&lt;Elem&gt;::clear()
  {
      LinkedNode&lt;Elem&gt; *indexPtr = head;
  
      while (head != NULL)
      {
          head = head-&gt;next;
          delete indexPtr;
          indexPtr = head;
      }
      listSize = 0;
  }
  
  //finds a search value in the list... if it finds it then it returns true,
  //otherwise it returns false
  template &lt;class Elem&gt;
  bool LinkedSortedList&lt;Elem&gt;::find(Elem searchValue) const
  {
      LinkedNode&lt;Elem&gt;* indexPtr = head;
      while (indexPtr != NULL)
      {
          if (indexPtr-&gt;value == searchValue)
          {
              return true;
          }
          indexPtr = indexPtr-&gt;next;
  
  
      }
      return false;
  }
  
  //gets and DELETES first value in the list - if it finds nothing then
  //return false, otherwise true
  template &lt;class Elem&gt;
  bool LinkedSortedList&lt;Elem&gt;::getFirst(Elem &amp;returnValue)
  {
      LinkedNode&lt;Elem&gt;* indexPtr = head;
      if (indexPtr == NULL)
          return false;
          else
          {
              head = head-&gt;next;
              returnValue = indexPtr-&gt;value;
              delete indexPtr;
              listSize--;
              return true;
          }
          returnValue = indexPtr-&gt;value;
  }
  
  //prints the list to cout or prints a warning if the list contains
  //no values
  template &lt;class Elem&gt;
  void LinkedSortedList&lt;Elem&gt;::print() const
  {
  
      if (head == NULL)
      {
           cout &lt;&lt; "No elements in the list" &lt;&lt; endl;
      }
          else
          {
              LinkedNode&lt;Elem&gt;* indexPtr = head;
              while (indexPtr != NULL)
              {
                  cout &lt;&lt; indexPtr-&gt;value &lt;&lt; endl;
                  indexPtr = indexPtr-&gt;next;
              }
          }
  }
  
  //returns the size of the list to the caller
  template &lt;class Elem&gt;
  int LinkedSortedList&lt;Elem&gt;::size() const
  {
      return listSize;
  }
  
  //inserts a value into the list where it should go, if the list is empty it will
  //say there are no existing nodes
  template &lt;class Elem&gt;
  bool LinkedSortedList&lt;Elem&gt;::insert(Elem newValue)
  {
      LinkedNode&lt;Elem&gt;* indexPtr = head;
      LinkedNode&lt;Elem&gt;* newNode;
      //newNode-&gt;value = newValue;
  
       try
      {
          newNode = new LinkedNode&lt;Elem&gt;(newValue);
      }
      catch(exception e)
      {
          cout&lt;&lt;"Exception reached: " &lt;&lt; e.what() &lt;&lt; endl;
          return false;
      }
  
      //checks to see if the list is empty, if it is then it makes the newNode
      //the head
      if (head == NULL)
          {
              cout &lt;&lt; "No existing nodes" &lt;&lt; endl;
              head = newNode;
              cout &lt;&lt; "First node is now " &lt;&lt; head-&gt;value &lt;&lt; endl;
              listSize++;
              return true;
          }
  
          /*looks to see if the value of the newNode is less than or equal to the
          index, if it is then it sets the point of the newNode equal to the head
          then makes the head the newNode and increments the listSize by one to keep
          track of the size of the list and returns true*/
          else if (newNode-&gt;value &lt;= head-&gt;value)
          {
              newNode-&gt;next = head;
              head = newNode;
              listSize++;
              return true;
          }
  
          /*if the newNode value is greater than the index, then:*/
          else
          {
              while(indexPtr-&gt;next != NULL &amp;&amp; newNode-&gt;value &gt; indexPtr-&gt;next-&gt;value)
                  {
                      indexPtr = indexPtr-&gt;next;
                  }
  
              if (indexPtr-&gt;next == NULL)
              {
                  indexPtr-&gt;next = newNode;
                  listSize++;
                  return true;
              }
              else
              {
                  newNode-&gt;next = indexPtr-&gt;next;
                  indexPtr-&gt;next = newNode;
                  listSize++;
                  return true;
              }
  
          }
  }
  //added for project 2 to return the head of the LL
  template &lt;class Elem&gt;
  LinkedNode&lt;Elem&gt;* LinkedSortedList&lt;Elem&gt;::getHead()
  {
      return head;
  }
  
  
  #endif
  </code></pre>
  
  <p>The Employee.cpp is:</p>
  
  <pre><code>#ifndef _Employee_
  #define _Employee_
  
  #include "Employee.h"
  #include "LinkedSortedList.h"
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  //blank default constructor
  Employee::Employee()
  {
  }
  
  //constructor that takes 3 parameters and sets them
  Employee::Employee(string lastName, string firstName, int eID)
  {
      this-&gt;lastName = lastName;
      this-&gt;firstName = firstName;
      this-&gt;empID = eID;
  }
  
  //blank deconstructor
  Employee::~Employee()
  {
  }
  
  //overloaded equality operator
  bool Employee::operator==(Employee &amp;nextEmployee)
  {
      if (this-&gt;empID == nextEmployee.empID)
              return true;
          else
              return false;
  }
  
  //overloaded less than or equal to operator
  bool Employee::operator &lt;= (Employee &amp;nextEmployee)
  {
      if (this-&gt;empID &lt;= nextEmployee.empID)
              return true;
          else
              return false;
  }
  
  //overloaded greater than or equal to operator
  bool Employee::operator &gt;= (Employee &amp;nextEmployee)
  {
      if (this-&gt;empID &gt;= nextEmployee.empID)
              return true;
          else
              return false;
  }
  
  //overloaded less than operator
  bool Employee::operator &lt; (Employee &amp;nextEmployee)
  {
      if (this-&gt;empID &lt; nextEmployee.empID)
              return true;
          else
              return false;
  }
  
  //overloaded greater than operator
  bool Employee::operator &gt; (Employee &amp;nextEmployee)
  {
      if (this-&gt;empID &gt; nextEmployee.empID)
              return true;
          else
              return false;
  }
  
  // overloaded output stream operator
  ostream&amp; operator&lt;&lt;(ostream&amp; os, const Employee empl)
  {
      os &lt;&lt; "Last: " &lt;&lt; empl.lastName &lt;&lt; endl;
      os &lt;&lt; "First: " &lt;&lt; empl.firstName &lt;&lt; endl;
      os &lt;&lt; "Employee ID: " &lt;&lt; empl.empID &lt;&lt; endl;
      return os;
  }
  
  #endif
  </code></pre>
  
  <p>My header file, LinkedSortedList.h:</p>
  
  <pre><code>#ifndef _LinkedSortedListClass_
  #define _LinkedSortedListClass_
  
  #include "SortedList.h"
  #include "LinkedNode.h"
  
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  //using namespace std;
  
  template &lt;class Elem&gt;
  class LinkedSortedList : public SortedList&lt; Elem &gt;
  {
  public:
      LinkedSortedList();
      ~LinkedSortedList();
      virtual void clear();
      virtual bool insert(Elem newValue);
      virtual bool getFirst(Elem &amp;returnValue);
      virtual void print() const;
      virtual bool find(Elem searchValue) const;
      virtual int size() const;
      LinkedNode&lt;Elem&gt;* getHead(); //added for project 2
  
  private:
      LinkedNode&lt;Elem&gt;* head;
      int listSize;
  };
  #endif
  </code></pre>
  
  <p>And finally (WHEW!) my Employee.h is here:</p>
  
  <pre><code>#ifndef _EmployeeClass_
  #define _EmployeeClass_
  
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  class Employee
  {
      public:
          Employee();
          ~Employee();
          Employee(string, string, int);
          bool operator == (Employee&amp; nextEmployee);
          bool operator &lt;= (Employee &amp;nextEmployee);
          bool operator &gt;= (Employee &amp;nextEmployee);
          bool operator &lt; (Employee &amp;nextEmployee);
          bool operator &gt; (Employee &amp;nextEmployee);
          friend ostream&amp; operator&lt;&lt;(ostream&amp; os, const Employee empl);
          string getLastName(){return lastName;}
          string getFirstName(){return firstName;}
          int getEmpID(){return empID;}
      private:
          string lastName;
          string firstName;
          int empID;
  };
  
  #endif
  </code></pre>
  
  <p>I'm just not seeing the problem here, I've spoken to my TA, the helproom staff and other students.  I haven't heard back from the prof yet, but I doubt I'll hear from her this weekend anyway.  Thank you VERY much for any insight you can provide.  I need some tylenol.
  -Josh</p>