Search code examples
c++lnk2019

Error LNK2019 simple Linked List implementation


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++.


Solution

  • 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