I am trying to make an ArrayList class with templates to store lists of objects. When I compile, it tells me
Error 2 error LNK2019: unresolved external symbol "public: void __thiscall ArrayList<int>::add(int *)" (?add@?$ArrayList@H@@QAEXPAH@Z) referenced in function _wmain C:\Users\Daniel\Documents\Dropbox\Dev\ListTest\ListTest\ListTest.obj ListTest
This doesn't make sense to me as I am not referencing external symbols, only ones I have included (I have included ArrayList.h). I have also tried with a test class, ClassA, and I get the same error (with different random characters on the function).
ArrayList.h:
[source lang="cpp"]#pragma once
template <class T>
class ArrayList
{
public:
ArrayList(void);
~ArrayList(void);
void add(T* item);
T* get(int i);
bool contains(T* item);
T* remove(int i);
void remove(T* item);
int count();
private:
void growIfShould();
void shrinkIfShould();
int size;
int arraySize;
T** items;
};
[/source]
ArrayList.cpp:
[source lang="cpp"]#include "stdafx.h"
#include "ArrayList.h"
template <class T>
ArrayList<T>::ArrayList(void)
{
arraySize = 16;
items = new T* [arraySize];
size = 0;
}
template <class T>
ArrayList<T>::~ArrayList(void)
{
}
template <class T>
void ArrayList<T>::add(T* item)
{
growIfShould();
items[size] = item;
size++;
}
template <class T>
T* ArrayList<T>::get(int i)
{
if (i < 0 || i >= size)
return null;
return items;
}
template <class T>
bool ArrayList<T>::contains(T* item)
{
for (int i=0;i<size;i++)
{
if (items == item)
return true;
}
return false;
}
template <class T>
T* ArrayList<T>::remove(int i)
{
if (i < 0 || i >= size)
return null;
T* temp = items;
items = null;
int j;
for (j = i; j < size - 1; j++)
{
items[j] = items[j + 1];
}
items[j] = null;
size--;
return temp;
}
template <class T>
void ArrayList<T>::remove(T* item)
{
for (int i=0;i<size;i++)
{
if (items == item)
{
remove(i);
break;
}
}
}
template <class T>
int ArrayList<T>::count()
{
return size;
}
template <class T>
void ArrayList<T>::growIfShould()
{
if (size >= arraySize)
{
arraySize *= 2;
T* objects = new T* [arraySize];
for (int i=0;i<size;i++)
{
objects = items;
}
delete [] items;
items = objects;
}
}
template <class T>
void ArrayList<T>::shrinkIfShould()
{
if (size < 2 * arraySize)
{
arraySize *= 2;
T* objects = new T* [arraySize];
for (int i=0;i<size;i++)
{
objects = items;
}
delete [] items;
items = objects;
}
}[/source]
ListTest.cpp:
[source lang="cpp"]// ListTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "ClassA.h"
#include "ArrayList.h"
int _tmain(int argc, _TCHAR* argv[])
{
ArrayList<int>* list = new ArrayList<int>();
for (int i=0;i<100;i++)
{
//list->add(new ClassA((i * 100) + i));
int x = i;
list->add(&x);
}
return 0;
}[/source]
Linker error with ArrayList
Long story short: you can't put the definition of template functions in a separate header without special compiler support that most compilers don't have or explicitly instantiating the template functions for each type you want to use. Just put the definitions in the header.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement