1

I have created a list and it worked perfectly, but I have to rewrite it using template, I have done this. But when I trying to compile my code I get Linker Error! Can you help with this code? Any help would be much appreciated! This is 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;
}

and 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);
}

and I get these errors:

Error 1 error LNK2019: unresolved external symbol "public: __thiscall >List::List(void)" (??0?$List@H@@QAE@XZ) referenced in function _main >c:\Users\lapchenko\documents\visual studio >2012\Projects\ConsoleApplication1\ConsoleApplication1\Source.obj

Error 2 error LNK2019: unresolved external symbol "public: __thiscall >List::~List(void)" (??1?$List@H@@QAE@XZ) referenced in function _main >c:\Users\lapchenko\documents\visual studio >2012\Projects\ConsoleApplication1\ConsoleApplication1\Source.obj

Error 3 error LNK2019: unresolved external symbol "public: void __thiscall >List::add(int &)" (?add@?$List@H@@QAEXAAH@Z) referenced in function _main >c:\Users\lapchenko\documents\visual studio >2012\Projects\ConsoleApplication1\ConsoleApplication1\Source.obj

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

Community
  • 1
  • 1
Alex Lapchenko
  • 283
  • 1
  • 5
  • 14

0 Answers0