1

I am trying to create a custom List in c++. I've defined it this way:

List.h:

#include "ListItem.h"

#pragma once
template<class T> class List
{
  private:
    ListItem<T>* first;
  public:
    T* GetAt(int);
    ListItem<T>* GetLastListItem();
    void Add(T*);
    void Clear();
};

List.cpp:

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

template<class T> T* List<T>::GetAt(int index)
{
    if (!first)
        return 0;

    ListItem<T>* current = first;

    for (int i = 1; i < index; i++)
    {
        current = current->GetNext();
    }

    return current->GetItem();
}

template<class T> L...

main:

List<TestItem> liste;
TestItem ti; //just a int inside.
liste.Add(&ti);

I am getting the following errors:

1>ConsoleApplication1.obj : error LNK2019: unresolved external symbol ""public: void __thiscall List::Add(class TestItem *)" (?Add@?$List@VTestItem@@@@QAEXPAVTestItem@@@Z)" in function"_main".

Andrew
  • 5,212
  • 1
  • 22
  • 40
Jan Wiesemann
  • 455
  • 2
  • 16
  • 5
    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) – drescherjm Apr 29 '16 at 14:09
  • Did you provide a definition of the `Add(T*)` member function? Is it visible at the point of instantiation (i.e. in `main`)? You probably need to define the implementations in your header file, or provide explicit instantiations for the types you wish to support. – Andrew Apr 29 '16 at 14:10

0 Answers0