【问题标题】:error C2244 unable to match function definition to an existing declaration错误 C2244 无法将函数定义与现有声明匹配
【发布时间】:2013-01-10 06:34:10
【问题描述】:

我正在尝试在 C++、Visual Studio 2010 中创建一个简单的模板列表 & 我得到了:错误 C2244 无法将函数定义与现有声明匹配。

我尝试将其更改为“typename”,但没有帮助。

这是一个基本的模板列表,具有非常基本的功能(Ctor、Dtor、Add、Delete)。

请帮忙。

#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

你有上面的代码。

【问题讨论】:

  • 你在哪里得到错误?

标签: c++ templates


【解决方案1】:

您在模板主体之外实现模板函数的语法不正确。应该是这样的:

template <typename T>
Node<T>::Node(T* n, Node<T>* head)
//  ^^^----- You need to add <T> here
{ 
    this->m_data = n;
    this->next=head;
}

您还缺少Node 的析构函数的定义:

template <typename T>
Node<T>::~Node()
{
    ... // Clean-up code 
}

Link to ideone.

【讨论】:

    猜你喜欢
    • 2012-11-06
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 1970-01-01
    • 2018-09-06
    相关资源
    最近更新 更多