我创建了一个列表并且它工作得很好,但是我必须使用模板重写它,我已经完成了。但是当我尝试编译我的代码时,我得到链接器错误! 你能帮忙解决这个问题吗?任何帮助将非常感激! 这是 List.h
#pragma once
#include <cstdlib>
template <class T>
class List
{
public:
struct Node
{
T data;
Node *next, *prev;
Node();
Node(T &smth);
~Node();
};
Node *head;
Node *tail;
public:
List();
~List();
void add(T &d);
void del(Node *);
};
List.cpp
#include "List.h"
template <class T>
List<T>::List()
{
}
template <class T>
List<T>::~List()
{
Node*n;
while (head != nullptr)
{
n = head->prev;
delete head;
head = n;
}
}
template <class T>
void List<T>::add(T &d)
{
Node *n = new Node(d);
n->prev = head;
n->next = nullptr;
if (head!=nullptr)
head->next = n;
if(head == nullptr){
head = n;
tail = head;
} else
head = n;
}
template <class T>
void List<T>::del(Node *node)
{
if (head == node)
head = node->prev;
if (tail == node)
tail = node->next;
if (node->next != nullptr)
node->next->prev = node->prev;
if (node->prev != nullptr)
node->prev->next = node->next;
delete node;
}
template <class T>
List<T>::Node::Node():data(),next(nullptr),prev(nullptr){}
template <class T>
List<T>::Node::Node(T &smth):data(smth),next(nullptr),prev(nullptr){}
template <class T>
List<T>::Node::~Node(){
data.~Word();
next = nullptr;
prev = nullptr;
}
和 main.cpp
#include <List.h>
#include <iostream>
using namespace std;
int main()
{
int a;
List<int> my;
cin >> a;
my.add(a);
cin >> a;
my.add(a);
cin >> a;
my.add(a);
}
我收到了这些错误:
错误1错误LNK2019:未解析的外部符号“public:__thiscall&gt; List :: List(void)”(?? 0?$ List @ H @@ QAE @ XZ)在函数_main&gt; c:\ Users中引用\ lapchenko \ documents \ visual studio&gt; 2012 \ Projects \ ConsoleApplication1 \ ConsoleApplication1 \ Source.obj
错误2错误LNK2019:未解析的外部符号“public:__thiscall&gt; List :: ~List(void)”(?? 1?$ List @ H @@ QAE @ XZ)在函数_main&gt; c:\中引用用户\ lapchenko \ documents \ visual studio&gt; 2012 \ Projects \ ConsoleApplication1 \ ConsoleApplication1 \ Source.obj
错误3错误LNK2019:未解析的外部符号“public:void __thiscall&gt; List :: add(int&amp;)”(?add @?$ List @ H @@ QAEXAAH @ Z)在function _main&gt; c中引用:\ Users \ lapchenko \ documents \ visual studio&gt; 2012 \ Projects \ ConsoleApplication1 \ ConsoleApplication1 \ Source.obj
错误4错误LNK1120:3个未解析的外部c:\ users \ lapchenko \ documents \ visual&gt; studio 2012 \ Projects \ ConsoleApplication1 \ Debug \ ConsoleApplication1.exe 1