0

I looked at the other questions already asked on SO regarding this same error message. They say to make sure my definition matches my implementation, and it does (I think). I have multiple classes, linkedListType (base template class), unorderedLinkedList (derived template class), main for testing it and a few others (can post snippets if needed).

When compiling it, I get this error:

Undefined symbols for architecture x86_64:
  "linkedListType<int>::deleteAtIndex(int&)", referenced from:
      deleteKthElement(unorderedLinkedList<int>&) in main.cpp.o
  "linkedListType<int>::getInfoAtIndex(int)", referenced from:
      printKthElement(unorderedLinkedList<int>&) in main.cpp.o
      deleteKthElement(unorderedLinkedList<int>&) in main.cpp.o
  "linkedListType<int>::linkedListType()", referenced from:
      unorderedLinkedList<int>::unorderedLinkedList() in main.cpp.o
  "linkedListType<int>::~linkedListType()", referenced from:
      unorderedLinkedList<int>::~unorderedLinkedList() in main.cpp.o
  "unorderedLinkedList<int>::deleteNode(int const&)", referenced from:
      vtable for unorderedLinkedList<int> in main.cpp.o
  "unorderedLinkedList<int>::insertLast(int const&)", referenced from:
      vtable for unorderedLinkedList<int> in main.cpp.o
  "unorderedLinkedList<int>::insertFirst(int const&)", referenced from:
      vtable for unorderedLinkedList<int> in main.cpp.o
  "unorderedLinkedList<int>::deleteSmallest()", referenced from:
      vtable for unorderedLinkedList<int> in main.cpp.o
  "unorderedLinkedList<int>::deleteAllOccurrences(int&)", referenced from:
      vtable for unorderedLinkedList<int> in main.cpp.o
  "unorderedLinkedList<int>::deleteFirstOccurrence(int&)", referenced from:
      vtable for unorderedLinkedList<int> in main.cpp.o
  "linkedListType<int>::printListReverse() const", referenced from:
      reversePrint(unorderedLinkedList<int>&) in main.cpp.o
  "linkedListType<int>::print() const", referenced from:
      print(unorderedLinkedList<int>&) in main.cpp.o
  "linkedListType<int>::length() const", referenced from:
      printKthElement(unorderedLinkedList<int>&) in main.cpp.o
      deleteKthElement(unorderedLinkedList<int>&) in main.cpp.o
  "unorderedLinkedList<int>::search(int const&) const", referenced from:
      vtable for unorderedLinkedList<int> in main.cpp.o

Looking at the first one (deleteAtIndex):

linkedListType.h:

template<class Type>
class linkedListType {
public:
   ...
   void deleteAtIndex(int& desiredIndex);
}

linkedListType.cpp:

template<class Type>
void linkedListType<Type>::deleteAtIndex(int& desiredIndex) {
    nodeType<Type> *temp = getNodeAtIndex(desiredIndex);
    nodeType<Type> *priorNode = getNodeAtIndex(desiredIndex-1);
    priorNode->link = temp->link;
    delete temp;
}

main.cpp:

// a convenience function in main.cpp... both 'getInfoAtIndex' and 'deleteAtIndex' only exist in the base class
void deleteKthElement(unorderedLinkedList<int> &ull){ 
    int elem;
    cout << "Enter an integer specifying the index to delete (0 - " << ull.length() - 1 << "): ";
    cin >> elem;
    int info = ull.getInfoAtIndex(elem);
    cout << "Info at index (" << elem << "): " << info << endl;
    ull.deleteAtIndex(elem);
    cout << "Node deleted." << endl;
}

I have no idea why these functions are giving me these errors.

Jeff
  • 4,285
  • 15
  • 63
  • 115
  • The implementation of the template should be in the header and not the cpp file. – Niall Oct 21 '16 at 21:41
  • @Niall why? I'm a bit confused since most of the time, I declare my functions in my header and implement them in the cpp files? – Jeff Oct 21 '16 at 21:45
  • 1
    I looked at this (http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). it looks like I can rename my cpps as tpp files? – Jeff Oct 21 '16 at 21:48
  • 1
    http://stackoverflow.com/q/495021/3747990. Most common convention is to put all the template code in the header. – Niall Oct 21 '16 at 21:49
  • @Jeff not with templates. The implementation needs to be instantiated somewhere, which usually means you want it in the header file. – davmac Oct 21 '16 at 21:49

0 Answers0