0

I am trying to make a simple linked list, and I came across a LNK2019 when I started to add code to my main function. It was compiling before, but when I added code to my main, I got several errors. I have all my header files included in my main CPP file, and I checked for circular includes. I haven't used C++ in a while, so sorry if this seems obvious. I went with the precompiled headers option when I made the project, if that means anything. What am I messing up?

Errors:

1>RandomCPP.obj : error LNK2019: unresolved external symbol "public: __thiscall List<int>::List<int>(void)" (??0?$List@H@@QAE@XZ) referenced in function _wmain
1>RandomCPP.obj : error LNK2019: unresolved external symbol "public: __thiscall List<int>::~List<int>(void)" (??1?$List@H@@QAE@XZ) referenced in function _wmain
1>RandomCPP.obj : error LNK2019: unresolved external symbol "public: void __thiscall List<int>::add(int const &)" (?add@?$List@H@@QAEXABH@Z) referenced in function _wmain
1>RandomCPP.obj : error LNK2019: unresolved external symbol "public: int * __thiscall List<int>::get(int)const " (?get@?$List@H@@QBEPAHH@Z) referenced in function _wmain

List.h:

#pragma once
#include "ListNode.h"

template<class T> class ListNode;

template<class T>
class List
{
public:
    List<T>();
    ~List<T>();

    void add(const T&);
    void insert(int, const T&);
    void clear();
    bool isEmpty() const;
    T* get(const int) const;
    int getLength() const;

    void foreach(void(*foo)(T)) const;

    T* operator[](const int index) const;

private:
    ListNode<T>* firstElement;
    ListNode<T>* lastElement;

    //Utility
    ListNode<T>* getNewNode(const T&);
    ListNode<T>* getNode(int index) const;
};

ListNode.h:

#pragma once

template<class T> class List;

template <class T>
class ListNode
{
    friend class List < T > ;

public:
    ListNode(const T& data);
    ~ListNode();

    T getData() const;

private:
    T data;
    ListNode<T> *nextPtr;
};

RandomCPP.cpp (Main File):

#include "stdafx.h"
#include "List.h"
#include "ListNode.h"

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    List<int> values = List<int>();
    values.add(0);
    values.add(1);
    values.add(2);
    values.add(3);
    values.add(4);

    cout << values.get(0) << endl
        << values.get(1) << endl
        << values.get(2) << endl
        << values.get(3) << endl
        << values.get(4) << endl << endl;

    system("pause");
    return 0;
}

stdafx.h:

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <iostream>

I have a List.cpp file and a ListNode.cpp file, each of which contain the definitions of the class declared in the corresponding .h file, and nothing else. The stdafx.cpp file is empty except an include of stdafx.h.

LW001
  • 2,452
  • 6
  • 27
  • 36
einsteinsci
  • 1,137
  • 1
  • 8
  • 22
  • 3
    (1) **Always** include the *exact* and *full* error message *verbatim* when reporting a questionable error. (2) There is no posted *implementation* of *any* of these template members (which should be in the same header file(s) as the template(s) if it is done right). Where is the actual implementations of all of these members? If they're in a separate set of *.cpp* files, [**read this**](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – WhozCraig Aug 24 '14 at 03:57
  • (1) Error list added in last edit. Forgot about that. (2) Didn't know they had to be in the header file. They're implemented in the classes' CPP files. Thanks! One question, why did this error only show up when I put stuff in my main? – einsteinsci Aug 24 '14 at 04:06
  • Now I'm getting a bunch of "Function template already defined" errors after changing the two CPP classes into TPP files, included at the end of their associated H files, both with #pragma once added at their starts. I don't have enough characters to post the errors, only the first one: `1>c:\users\einsteinsci\documents\visual studio 2013\projects\randomcpp\randomcpp\list.tpp(89): error C2995: 'void List::insert(int,const T &)' : function template has already been defined` – einsteinsci Aug 24 '14 at 04:17
  • 1
    Lose the layered includes and put the template member implementations in their respective header files *after* the declarations of they respective class template declarations. There is no reason to, and no benefit in, separating the declaration and definition in separate files. – WhozCraig Aug 24 '14 at 04:19
  • Glad to help. Take the to read the question/answers for which this is marked as a duplicate. It will help describe not only why the error you were getting was happening, but also helps better understands how the compiler utilizes templates in your code. – WhozCraig Aug 24 '14 at 19:50

0 Answers0