I'm trying to create a simple template list in C++, Visual Studio 2010 & I'm the getting : error C2244 unable to match function definition to an existing declaration.
I've tried to change it to 'typename' but it didn't help.
it's a basic template list with the very basic functions ( Ctor,Dtor,Add,Delete).
Please help.
#ifndef LIST_H_
#define LIST_H_
template <typename T>
class Node
{
T* m_data;
Node* next;
public:
Node(T*, Node<T>*);
~Node();
void Delete (Node<T>* head);
};
template <typename T>
Node::Node(T* n, Node<T>* head)
{
this->m_data = n;
this->next=head;
}
template <typename T>
void Node::Delete(Node<T>* head)
{
while(head)
{
delete(head->m_data);
//head->m_data->~data();
head=head->next;
}
}
template <typename T>
class List
{
Node<T*> head;
public:
List();
~List();
void addInHead (T*);
};
template <typename T>
void List :: addInHead (T* dat)
{
head = new Node<T*> (dat,head);
}
template <typename T>
List::List()
{
head = NULL;
}
template <typename T>
List :: ~List()
{
head->Delete(head);
}
#endif
You have the code above.
Your syntax for implementing template functions outside of template body is incorrect. It should be like this:
template <typename T>
Node<T>::Node(T* n, Node<T>* head)
// ^^^----- You need to add <T> here
{
this->m_data = n;
this->next=head;
}
You are also missing a definition of the destructor for Node
:
template <typename T>
Node<T>::~Node()
{
... // Clean-up code
}
Link to ideone.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With