0

I'm creating a simple sortedLL, right now I'm struggling using templates in C++. I am getting an error when compiling, which I'm attaching at the end of this post. Am I missing/coding something wrong when using templates?

sortedLL.h

template <class T>
class sortedLL {
private:
    struct Node
    {
        T data;
        int count;
        Node * next;
        Node (T data)
        {
            this->data = data;
            count = 1;
            next = NULL;
        }
    };
    Node* head;
    int lengthLL;
    Node* search(T value);
    void sortLL();
public:
    sortedLL();

    void insert(T value);
    void printLL();


};

sortedLL.cpp

#include "sortedLL.h"

template <class T>
sortedLL<T>::sortedLL()
{
    head = NULL;
    lengthLL = 0;
}

template <class T>
void sortedLL<T>::insert(T value)
{
    if (head == NULL)
        head = new Node (value);
    else
    {
        Node* searchNode = search(value);
        if (searchNode != NULL)
        {
            searchNode->count++;
            sortLL();
        }
        else
        {
            Node* newHead = new Node(value);
            newHead->next = head;
            head = newHead;
        }
    }

}

template <class T>
typename sortedLL<T>::Node * sortedLL<T>::search(T value)
{
    Node* currentNode = head;
    while (currentNode != NULL)
    {
        if (currentNode->data == value)
            return currentNode;
        else
            currentNode = currentNode->next;
    }
    return NULL;
}
template <class T>
void sortedLL<T>::printLL()
{
    if (head == NULL)
        std::cout << std::endl;
    else
    {
        Node* currentNode = head;
        while (currentNode != NULL)
        {
            std::cout << currentNode->data << ": " << currentNode->count << std::endl;
            currentNode = currentNode->next;
        }
    }
}

template<class T>
void sortedLL<T>::sortLL() {
    //Perform sort here 
}

main.cpp

#include <iostream>
#include "sortedLL.h"
int main() {

    sortedLL <int> LL;
    LL.insert(1);
    LL.insert(2);
    LL.insert(4);
    LL.printLL();
    std::cout << std::endl;
    LL.insert(1);
    LL.printLL();

    return 0;
}

Error

Screenshot of the error

Here is the error in text:

Scanning dependencies of target sortedLL [ 66%] Building CXX object CMakeFiles/sortedLL.dir/main.cpp.o [ 66%] Building CXX object CMakeFiles/sortedLL.dir/sortedLL.cpp.o [100%] Linking CXX executable sortedLL Undefined symbols for architecture x86_64:   "sortedLL<int>::insert(int)", referenced from:
      _main in main.cpp.o   "sortedLL<int>::printLL()", referenced from:
      _main in main.cpp.o   "sortedLL<int>::sortedLL()", referenced from:
      _main in main.cpp.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make[3]: *** [sortedLL] Error 1 make[2]: *** [CMakeFiles/sortedLL.dir/all] Error 2 make[1]: *** [CMakeFiles/sortedLL.dir/rule] Error 2 make: *** [sortedLL] Error 2
Gboi
  • 1
  • 1

0 Answers0