1

我创建了一个列表,它运行良好,但我必须使用模板重写它,我已经做到了。但是当我尝试编译我的代码时,我得到了链接器错误!你能帮忙看看这段代码吗?任何帮助将非常感激!这是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 *);

};

列表.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:函数 _main >c:\Users\lapchenko\documents\ 中引用的未解析外部符号“public: __thiscall >List::List(void)”(??0?$List@H@@QAE@XZ)视觉工作室>2012\Projects\ConsoleApplication1\ConsoleApplication1\Source.obj

错误 2 错误 LNK2019:函数 _main >c:\Users\lapchenko\documents 中引用的未解析外部符号“public: __thiscall >List::~List(void)”(??1?$List@H@@QAE@XZ) \visual studio >2012\Projects\ConsoleApplication1\ConsoleApplication1\Source.obj

错误 3 错误 LNK2019:函数 _main >c:\Users\lapchenko\ 中引用的未解析外部符号“public: void __thiscall >List::add(int &)” (?add@?$List@H@@QAEXAAH@Z)文档\visual studio >2012\Projects\ConsoleApplication1\ConsoleApplication1\Source.obj

错误 4 error LNK1120: 3 unresolved externals c:\users\lapchenko\documents\visual >studio 2012\Projects\ConsoleApplication1\Debug\ConsoleApplication1.exe 1

4

0 回答 0