Reputation: 88
I'm a Computer Science student and we're currently studying C++ Templates
.
We were instructed to write a Linked List
template class and we are not allowed to use STL
(note that this is unfinished code):
template <class T>
class LinkedList
{
private:
//////// NODE CLASS //////////
template <class T>
class Node
{
public:
///// DATA MEMBERS
T *m_val;
Node<T> *m_next;
Node<T> *m_prev;
// CTORS
Node(T *val) : m_val(val) { m_next = NULL; m_prev = NULL; }
Node(T *val, Node<T> *next, Node<T> *prev) : m_val(val), m_next(next), m_prev(prev) { }
// C.CTOR
Node(const Node<T> & n) { m_val = n->m_val; m_next = n->m_next; m_prev = n->m_prev; }
// DTOR
~Node() { delete m_val; if (m_prev) m_prev->m_next = m_next; if (m_next) m_next->m_prev = m_prev; }
};
///////////////////////
Node<T> *first;
public:
LinkedList(T*)
{
}
~LinkedList();
};
template <class T>
LinkedList<T>::LinkedList(T* t)
{
first = Node<T>(t); /// <---- PROBLEM HERE
}
However, the problem is that the compiler doesn't let me use the Node
Constructor..
What am I doing wrong?
EDIT:
As I said, this is unfinished code, so I thought the error I'm getting would be confusing (because I couldn't see directly where the problem comes from).
Anyway, Here is the error (Debugging on Microsoft Visual C++ 2010 Express):
1>------ Build started: Project: Ex4, Configuration: Debug Win32 ------
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>c:\users\dan\documents\visual studio 2010\Projects\Ex4\Debug\Ex4.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Upvotes: 1
Views: 109
Reputation: 206717
The line
first = Node<T>(t);
needs to be
first = new Node<T>(t);
since first
is a Node*
, not a Node
.
You can also initialize it using:
template <class T>
LinkedList<T>::LinkedList(T* t) : first(new Node<T>(t))
{
}
which is a preferred approach.
Upvotes: 1