Jump to content

View more

Image of the Day

Boxes as reward for our ranking mode. ヾ(☆▽☆)
#indiedev #gamedev #gameart #screenshotsaturday https://t.co/ALF1InmM7K
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Linker error with ArrayList

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
2 replies to this topic

#1 viper110110   Members   

1111
Like
0Likes
Like

Posted 27 November 2012 - 09:51 PM

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 oncetemplate <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[i];}template <class T>bool ArrayList<T>::contains(T* item){ for (int i=0;i<size;i++) { if (items[i] == item) return true; } return false;}template <class T>T* ArrayList<T>::remove(int i){ if (i < 0 || i >= size) return null; T* temp = items[i]; items[i] = 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[i] == 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[i] = items[i]; } 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[i] = items[i]; } 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]

#2 SiCrane   Moderators   

11760
Like
1Likes
Like

Posted 27 November 2012 - 10:03 PM

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.

#3 viper110110   Members   

1111
Like
0Likes
Like

Posted 27 November 2012 - 11:29 PM

Well that's really dumb. It works now though, thanks.




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.