0

I'm trying to make a templated node for a linked list but get error LNK2019: unresolved external symbol when I attempt to build the solution. This only happens when I try to create an instance of the Node in Main.

Node.h:

#ifndef Node_A
#define Node_A

template <class T>
class Node
{
public:
 Node();
 ~Node();
 T getData();
 Node* getNext();
 void setData(T);
 void setNext(Node*);

private:
 Node *next;
 T data;
};
#endif

Node.cpp

#include "Node.h"

template <class T>
Node<T>::Node()
{
 next = NULL;
 return;
}

template <class T>
Node<T>::~Node()
{
 return;
}

...irrelevant

Main.cpp

#include <iostream>
#include "Node.h"

using namespace std;

int main()
{
 Node<int> a,b;
 //Node<int> *ptr;

 /*...*/

 return 0;
}

Error Message:

1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Node<int>::Node<int>(void)" (??0?$Node@H@@QAE@XZ) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Node<int>::~Node<int>(void)" (??1?$Node@H@@QAE@XZ) referenced in function _main
  • possible duplicate of [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Praetorian Mar 06 '14 at 23:10

1 Answers1

0

Your function template and template class member function definitions have to go in the header file along with their declarations. Unless you are using extern templates, the definition of a function template or template class member function is required in every translation unit that uses it.

Simple
  • 13,992
  • 2
  • 47
  • 47