0

I am implementing a simple Linked List, but I keep getting the LNK2019 error, I simplified my code to the minimum to track the problem,but I keep getting it. I am using Visual Studio 2010. My header file is:

#ifndef __TSLinkedList__H__
#define __TSLinkedList__H__

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "LinkedNode.h"

template <class T>
class LinkedList
{
public:
LinkedList(void);
~LinkedList(void);
protected:
LinkedNode<T> * head;
};

The implementation file is:

#include "StdAfx.h"
#include "LinkedList.h"

template <class T>
LinkedList<T>::LinkedList(void)
{
head = NULL;
}
template <class T>
LinkedList<T>::~LinkedList(void)
{
}

the main function is:

#include "stdafx.h"
#include "stdlib.h"
#include "LinkedList.h"

int _tmain(int argc, _TCHAR* argv[])
{
LinkedList<int> mList;
system("PAUSE");
return 0;
} 

and I am getting this error:

Error 1 error LNK2019: símbolo externo "public: __thiscall LinkedList::~LinkedList(void)" (??1?$LinkedList@H@@QAE@XZ) in function _wmain

I get the same error with the Constructor. The funny thing is that it is pointing to _wmain, and my main function is called _tmain. I already tried to change Subsystem linker from /SUBSYSTEM:WINODWS to /SUBSYSTEM:CONSOLE, but it was already set up as /SUBSYSTEM:CONSOLE. Obviously my implementation does a lot more than this, but I ripped out all of it to track this problem. Help wpuld be apreciated, this is driving me nuts. I am new to C++.

amaurs
  • 1,622
  • 1
  • 13
  • 18
  • OK more question about this.. just move the implementation in the header file. Templates **cannot** be separated in header/source files. – Kiril Kirov Jul 12 '12 at 14:53
  • 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) – Mike Seymour Jul 12 '12 at 14:55
  • 1
    Also, you shouldn't use [reserved names](http://stackoverflow.com/q/228783/204847) as include guards. – Mike Seymour Jul 12 '12 at 14:56

2 Answers2

2

Move the function implementations to the header file.

In order to generate code for the specialization, the compiler must have the definitions of the functions available to each translation unit.

#ifndef __TSLinkedList__H__
#define __TSLinkedList__H__

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "LinkedNode.h"
template <class T>
class LinkedList
{
public:
    LinkedList(void);
    ~LinkedList(void);
    protected:
    LinkedNode<T> * head;
};

template <class T>
LinkedList<T>::LinkedList(void)
{
head = NULL;
}
template <class T>
LinkedList<T>::~LinkedList(void)
{
}

#endif
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

The compiler doesn't compile your template class member definitions, since they are not included in any compilation unit.

However, it does see that some members are used, so it will generate 'undefined' symbols for these.

Then comes the linker, trying to match the undefined symbols to some symbols defined in one of the compiled object files.

But, the LinkedList::~LinkedLis() destructor hasn't been compiled, so it's in none of the object files, and that's what the linker complains about.

You can fix this by

  • including the definition file in the main source file,
  • or pasting the definitions inside the template header file,
  • or including the template implementation file from the bottom of the template header file (my favorite)
xtofl
  • 40,723
  • 12
  • 105
  • 192