0

All, I am implementing a LinkedList and am experiencing the following error. As far as I searched, this hasn't been asked before. What does unresolved external symbol mean? This is a Win32 console application. Any help would be appreciated. Apologies in advance for the code formatting. I haven't figured it out yet.

LNK2019 unresolved external symbol "public: void __thiscall List::push_front(int const &)" (?push_front@?$List@H@@QAEXABH@Z) referenced in function _main

List.cpp

 

#include "stdafx.h"
#include "List.h"
#include "Node.h"

template <typename Type>
List<Type>::List()
{
    head = NULL;
    tail = NULL;
    count = 0;
}

template <typename Type>
List<Type>::~List()
{
}

template <typename Type>
bool List<Type>::empty()
{
    return count == 0;
}

template <typename Type>
int List<Type>::size()
{
    return count;
}

template <typename Type>
void List<Type>::push_front(const Type &d)
{
    Node *new_head = new Node(d, NULL, head);

    if (this->empty())
    {
        head = new_head;
        tail = new_head;
    }
    else
    {
        head->prev = new_head;
        head = new_head;
    }

    count++;
}

template <typename Type>
void List<Type>::push_back(const Type &d)
{
    Node *new_tail = new Node(d, NULL, tail);

    if (this->empty())
    {
        head = new_tail;
        tail = new_tail;
    }
    else
    {
        tail->next = new_tail;
        tail = new_tail;
    }

    count++;
}

template <typename Type>
void List<Type>::DisplayContents()
{
    Node *current = head;
    for (int i = 0; i <= this->size; i++)
    {
        cout << current->data << " ";
        current = current->next;
    }
}
</pre></code>

LinkedList.cpp

#include "stdafx.h" #include "List.h" int main() { List<int> listIntegers; listIntegers.push_front(10); listIntegers.push_front(2011); listIntegers.push_back(-1); listIntegers.push_back(9999); listIntegers.DisplayContents(); return 0; }
user84405
  • 23
  • 1
  • 5
  • You'll need to make the `List::push_back` definition visible where it's instantiated so the compiler can generate the `List::push_back` function. Note that `template`s aren't instantiated until they're used or they're explicitly instantiated. The recommended way to do it is to put the entire class template definition in a header to be included by main.cpp, or explicitly instantiating `List` somewhere so the linker can link to an external symbol (not recommended unless you know there are a fixed set of types that `List` will ever be used with, and even then maybe not). – mkal Jun 27 '16 at 02:25
  • What is the "external symbol" that the error message is talking about? What does it mean? – user84405 Jun 27 '16 at 02:43
  • the error message means that the compiler had no way of generating the function in `main.cpp`, so it instead assumed the function instantiation exists external to itself (i.e., in another compilation unit). Therefore it put a reference to the symbol where it's used so the linker can later load the external reference with an actual memory location after all compilation units are done. Because the function wasn't instantiated anywhere, the linker couldn't find a matching symbol. – mkal Jun 27 '16 at 03:09

0 Answers0