I'm learning basic Linked Lists and wanted to try my hand at writing my own implementation. However, I keep getting the following errors when compiling:
/tmp/ccNerPXr.o: In function `main':
main.cpp:(.text+0x10): undefined reference to `LinkedList<int>::LinkedList()'
main.cpp:(.text+0x2c): undefined reference to `LinkedList<int>::insert(int const&)'
main.cpp:(.text+0x49): undefined reference to `LinkedList<int>::displayList()'
collect2: error: ld returned 1 exit status
I've looked at other stack overflow answers and couldn't find one to meet my specific issue. The name of the template class matches the instance declaration in main, I remembered to #include "LinkedList.h" properly, and I double-checked to make sure the forward declarations in *.h matched the ones in *.cpp. What else could be causing this error? Is it staring me in the face, and I'm just too blind to see it? I feel like this might be simpler than what I'm making it...
Here is my (unfinished) source code for reference: main.cpp
#include "LinkedList.h"
#include <iostream>
using namespace std;
int main()
{
LinkedList<int> normList;
for(int i = 0; i < 5; i++)
normList.insert(i);
normList.displayList();
return 0;
}
LinkedList.h
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <cstddef>
template <class Object>
class LinkedList
{
struct ListNode
{
Object element;
ListNode *next;
ListNode() : element(0), next(NULL) {};
};
ListNode *head;
ListNode *tail;
public:
LinkedList();
void insert(const Object &x);
void erase(const Object &x);
void eraseList();
void displayList();
};
#endif
LinkedList.cpp
#include "LinkedList.h"
#include <iostream>
using namespace std;
template <class Object>
LinkedList<Object>::LinkedList()
{
head = NULL;
}
/*This function will insert an object
*using the tail-end insertion method
*i.e., inserting at the end of the list
*/
template <class Object>
void LinkedList<Object>::insert(const Object &x)
{
ListNode *newNode = new ListNode(x, NULL);
if( !head ) {
head = newNode;
tail = head;
} else {
tail->next = newNode;
tail = newNode;
}
}//insert()
template <class Object>
void LinkedList<Object>::erase(const Object &x)
{
ListNode *prev;
} //erase()
/*
*Function that deletes entire list
*/
template <class Object>
void LinkedList<Object>::eraseList()
{
ListNode *prev;
while(head->next)
{
prev = head;
delete head;
head = prev->next;
}
}//eraseList()
template <class Object>
void LinkedList<Object>::displayList()
{
if(!head)
cout << "The list is empty\n";
while(head->next)
{
cout << head->element << endl;
head = head->next;
}
} //displayList()
Thanks in advance to any help or input