1

As the title suggests, I am trying to learn data structures and I am starting with a linked list. I decided to make it generic since I thought it would be useful to do to handle other data types.

I am getting these associated errors:

enter image description here

I am not sure what I am doing wrong and I cannot identify the error. Here is my code starting with the header file:

#ifndef LinkedList_hpp
#define LinkedList_hpp

template<class T>
struct Node {
    T data;
    Node<T>* next;
};

template<class T>
class SingleLinkedList {
private:
    Node<T>* head, tail;
public:
    SingleLinkedList();
    void createNode(const T& theData);
    void display();
    void insert_start(const T& theData);
    void insert_position(int pos, const T& theData);
    void delete_first();
    void delete_last();
    void delete_position(int pos);
    Node<T>* search(Node<T>* head, const T& target);
};

#endif /* LinkedList_hpp */

Now here is the associated .cpp file:

#include "LinkedList.hpp"
#include <iostream>

template<class T>
SingleLinkedList<T>::SingleLinkedList() {
    head = nullptr;
    tail = nullptr;
}

template<class T>
void SingleLinkedList<T>::createNode(const T& theData) {
    Node<T>* temp = new Node<T>;
    temp->data = theData;
    temp->next = nullptr;
    if(head == nullptr) {
        head = temp;
        tail = temp;
        temp = nullptr;
    }
    else {
        tail->next = temp;
        tail = temp;
    }
}

template<class T>
void SingleLinkedList<T>::display() {
    Node<T>* temp = new Node<T>;
    temp = head;
    while(temp != nullptr) {
        std::cout << temp->data << "\t";
        temp = temp->next;
    }
}

template<class T>
void SingleLinkedList<T>::insert_start(const T& theData) {
    Node<T>* temp = new Node<T>;
    temp->data = theData;
    temp->next = head;
    head = temp;
}

template<class T>
void SingleLinkedList<T>::insert_position(int pos, const T &theData) {
    Node<T>* previous = new Node<T>;
    Node<T>* current = new Node<T>;
    Node<T>* temp = new Node<T>;
    temp = head;
    for(int i  = 1; i < pos; i++) {
        previous = current;
        current = current->next;

    }
    temp->data = theData;
    previous->next = temp;
    temp->next = current;
}

template<class T>
void SingleLinkedList<T>::delete_first() {
    Node<T>* temp = new Node<T>;
    temp = head;
    head = head->next;
    delete temp;
}

template<class T>
void SingleLinkedList<T>::delete_last() {
    Node<T>* previous = new Node<T>;
    Node<T>* current = new Node<T>;
    current = head;
    while(current->next != nullptr) {
        previous = current;
        current = current->next;
    }
    tail = previous;
    previous->next = nullptr;
    delete current;
}

template<class T>
void SingleLinkedList<T>::delete_position(int pos) {
    Node<T>* previous = new Node<T>;
    Node<T>* current = new Node<T>;
    current = head;
    for(int i = 1; i < pos; i++) {
        previous = current;
        current = current->next;
    }
    previous->next = current->next;
}

And finally here is the main.cpp file where I attempt to test the code:

#include <iostream>
#include "LinkedList.hpp"


int main(int argc, const char * argv[]) {

    SingleLinkedList<int> obj;
    obj.createNode(2);
    obj.createNode(4);
    obj.createNode(6);
    obj.createNode(8);
    obj.display();



    return 0;
}
  • Possible duplicate of [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Guillaume Racicot May 22 '18 at 01:42

1 Answers1

1

You need to put the implementation of your member functions into the header file.

  • Oh so the .cpp file associated with it is completely useless? –  May 22 '18 at 01:33
  • Maybe.Because the compiler need to see the definition when instantiating the template.There is another question the same as yours with many answers.You can look at it. –  May 22 '18 at 01:41
  • @Snorrlaxxx yes the cpp file is pretty useless for templates, unless you know and list every possible instantiations in the cpp, and declare them as extern template in the header. For a generic linked list, I would advise to put the implementation in the header. See my comment on your question. – Guillaume Racicot May 22 '18 at 01:44