Hi all,
I'm new in C++ and I encounter a problem with a code I found on this forum.
I simply try to add an integer to a Generic Doubly-Linked list. However, when I launch the following code (main function)
DoubleLinked<int> dlist;
dlist.push_back(1);
I got the following error :
"DoubleLinked<int>::push_back(int)", referenced from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Here is the code I used for the Generic Double-Linked list :
double_linked.h
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include <iostream>
template <typename T>
class DoubleLinked
{
struct Node
{
T data;
Node* prev;
Node* next;
Node(T t, Node* p, Node* n) : data(t), prev(p), next(n) {}
};
Node* head;
Node* tail;
public:
DoubleLinked() : head( NULL ), tail ( NULL ) {}
bool empty() const { return ( !head || !tail ); }
operator bool() const { return !empty(); }
void push_back(T);
void push_front(T);
T pop_back();
T pop_front();
~DoubleLinked()
{
while(head)
{
Node* temp(head);
head=head->next;
delete temp;
}
}
};
#endif
double_linked.cpp
#include "double_linked.h"
template <typename T>
void DoubleLinked<T>::push_back(T data)
{
tail = new Node(data, tail, NULL);
if( tail->prev )
tail->prev->next = tail;
if( empty() )
head = tail;
}
template <typename T>
void DoubleLinked<T>::push_front(T data)
{
head = new Node(data, NULL, head);
if( head->next )
head->next->prev = head;
if( empty() )
tail = head;
}
template<typename T>
T DoubleLinked<T>::pop_back()
{
if( empty() )
throw("DoubleLinked : list empty");
Node* temp(tail);
T data( tail->data );
tail = tail->prev ;
if( tail )
tail->next = NULL;
else
head = NULL ;
delete temp;
return data;
}
template<typename T>
T DoubleLinked<T>::pop_front()
{
if( empty() )
throw("DoubleLinked : list empty");
Node* temp(head);
T data( head->data );
head = head->next ;
if( head )
head->prev = NULL;
else
tail = NULL;
delete temp;
return data;
}
Could someone help me?
Thanks in advance