Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error C2244 unable to match function definition to an existing declaration

Tags:

c++

templates

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.

like image 288
user2013420 Avatar asked Jan 26 '13 11:01

user2013420


1 Answers

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.

like image 192
Sergey Kalinichenko Avatar answered Oct 13 '22 12:10

Sergey Kalinichenko