I've searched online and found several topics with the same error, but I still can't figure this out. I think the issue might be might with #includes.
I first thought I had to remove the #include dlinkedlist.h from the cpp file, but then that prevents syntax highlighting and causes syntax errors. However, if I do include it, the syntax highlight returns but the function template already define error comes up.
///////////////////////////////////
//////dlinkedlist.h////////////////
///////////////////////////////////
#ifndef _DLINKEDLIST_H_
#define _DLINKEDLIST_H_
#include <cstdlib>
#include <stdexcept>
#include <string>
usingnamespace std;
// template class for doubly-linked list node
template <class T>
class Node
{
public:
T data;
//string data;
Node<T>* prev;
Node<T>* next;
// default constructor
template <class T>
Node(T value)
{
data = value;
prev = NULL;
next = NULL;
}
};
// DLinkedList class definition
template <class T>
class DLinkedList
{
private:
// DLinkedList private members
int size; // number of items stored in list
Node<T>* front; // references to the front
Node<T>* back; // and back of the list
// helper function for deep copy
// Used by copy constructor and operator=
void CopyList(const DLinkedList& ll);
// helper function for deep delete
// Used by destructor and copy/assignment
void DeleteList();
public:
// default constructor
DLinkedList();
// copy constructor, performs deep copy of list elements
DLinkedList(const DLinkedList& ll);
};
#include "dlinkedlist.cpp"
#endif
#include <cstdlib>
#include <stdexcept>
#include <string>
usingnamespace std;
// template class for doubly-linked list node
template <class T>
class Node
{
public:
T data;
//string data;
Node<T>* prev;
Node<T>* next;
// default constructor
Node(T value)
{
data = value;
prev = NULL;
next = NULL;
}
};
// DLinkedList class definition
template <class T>
class DLinkedList
{
private:
// DLinkedList private members
int size; // number of items stored in list
Node<T>* front; // references to the front
Node<T>* back; // and back of the list
// helper function for deep copy
// Used by copy constructor and operator=
void CopyList(const DLinkedList& ll);
// helper function for deep delete
// Used by destructor and copy/assignment
void DeleteList();
public:
// default constructor
DLinkedList();
// copy constructor, performs deep copy of list elements
DLinkedList(const DLinkedList& ll);
void InsertFront(T item);
};
#include "source.cpp"
Thanks for the suggestion Jaybobo. Your method was how I would've done it, but unfortunately my assignment requires the implementation to be in a cpp file.
I dont like this approach, including cpp files looks bad.
you cant put a template in a cpp file and compile it. well you can but it will be useless.
when you want to use the linked list in another class you will need to include the source for the entire template so the compiler can compile it with the template parameter. it will need the entire template class so you will need to include the cpp also.
this is why template libraries are usually header only.