What's wrong with this simple code?!

I have a pretty simple Doubly Linked List implementation (only the constructor and destructor so far), but I get this error (ide is VS 2008):

Error 1:
Description:

error LNK2019: unresolved external symbol "public: __thiscall CustomList<int>::~CustomList<int>(void)" (??1?$CustomList@H@@QAE@XZ) referenced in function _main
File:
driverCustom.obj

Error 2:
Description:

error LNK2019: unresolved external symbol "public: __thiscall CustomList<it>::CustomList<int>(void)" (??0?$CustomList@H@@QAE@XZ) referenced in function _main
File:
driverCustom.obj


I know the error has to do with my use of template. The weird thing is that it compiles perfectly when I just copy-paste the constructor and destructor code into their respective declarations, so I don't think anything is wrong with the actual code.

Here are my files: CustomList.h (interface), CustomList.cpp (implementation), driverCustom.cpp (driver).

Thank you very much for any help!!!
<b>CustomList.h</b>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;

template <typename Object>
class CustomList
{
public:
	CustomList();
	~CustomList();
	
	void insert(const Object & value);
	void remove(const Object & value);
	void contains(const Object & value) const;
	
	const Object & findMin() const;
	const Object & findMax() const;
	int size();

private:
	struct Node
	{
		Object element;
		Node *next;
		Node *prev;

		Node(const Object & e= Object(), Node *n= NULL, Node *p= NULL)
			: element(e), next(n), prev(p) { }
	};

	Node *head;
	Node *tail;
	Node *p;
	int currentSize;
};
CustomList.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "CustomList.h"

template <typename Object>
CustomList<Object>::CustomList()
{
	head= new Node();
	tail= new Node();
	head->next= tail;
	p= NULL;
	currentSize= 0;
}

template <typename Object>
CustomList<Object>::~CustomList()
{	
	p= head->next;
	while (p != tail) {
		p= p->next;
		delete head->next;
		head->next= p;
	}
	delete head;
	delete tail;
}
You can't divide template definition from it's declaration, you'll have to keep everything in the header
http://www.cplusplus.com/forum/articles/14272/
http://www.cplusplus.com/forum/articles/10627/ -section 8-
Last edited on
driverCustom.cpp

1
2
3
4
5
6
#include "CustomList.h"

void main()
{
	CustomList<int> linked;
}
WOW that was fast!

I still don't understand what the problem is. In this case would I need implicit or explicit instantiation? According to the first link it is possible to split in into 2 files using explicit instantiation, however they state types "int" and "float" at the end. Does this not allow for other types (i.e. string) to be used?

From what I understand, I need implicit instantiation (so move everything to the header)...is this right?
According to the first link it is possible to split in into 2 files using explicit instantiation, however they state types "int" and "float" at the end. Does this not allow for other types (i.e. string) to be used? Nope, if you explicitly instantiate for int and float you can use only int and float
From what I understand, I need implicit instantiation (so move everything to the header)...is this right?
Yes
thank you, solved!
Topic archived. No new replies allowed.