Template Class

I'm getting something like:

1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall DynamicClass<int>::~DynamicClass<int>(void)" (??1?$DynamicClass@H@@QAE@XZ) referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall DynamicClass<int>::DynamicClass<int>(void)" (??0?$DynamicClass@H@@QAE@XZ) referenced in function _main
1>C:\Users\Tristan\documents\visual studio 2010\Projects\DynamicArray\Debug\DynamicArray.exe : fatal error LNK1120: 2 unresolved externals


after making the ff. template class and instantiating it. What could be the problem?

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#pragma once

#include <iostream>
template<typename T>
class DynamicClass
{
public:
	DynamicClass();
	~DynamicClass();
	DynamicClass(int size);
	DynamicClass(const DynamicClass<T>& rhs);
	DynamicClass<T>& operator=(const DynamicClass<T>& rhs);
	void resize(int newSize);
	T& operator[](int i);
	int size();
	


private:
	T* mData;
	int mSize;
};


#include "DynamicClass.h"

template<typename T>
DynamicClass<T>::DynamicClass()
{
	mSize=0;
	mData=new T[0];
}


template<typename T>
DynamicClass<T>::DynamicClass(int size)
{
	mSize=size;
	mData=new T[mSize];
}

template<typename T>
T& DynamicClass<T>::operator[](int i)
{
	return mData[i];
}

template<typename T>
void DynamicClass<T>::resize(int newSize)
{
	T* newArray=new T[newSize];

	if(newSize>=mSize)
	{
		for(int i=0;i<mSize)
		{
			newArray[i]=mData[i];
		}
	}

	else if(newSize<mSize)
	{
		for(int i=0;i<newSize;++i)
		{
			newArray[i]=mData[i];
		}
	}

	delete[] mData;
	mData=newArray;
	mSize=newSize;
}

template<typename T>
DynamicClass<T>::DynamicClass(const DynamicClass<T>& rhs)
{
	T* newArray=new T[rhs.mSize];
	mSize=rhs.mSize;

	for(int i=0;i<rhs.mSize;++i)
	{
		mData[i]=rhs.mData[i];
	}
}

template<typename T>
DynamicClass<T>& DynamicClass<T>::operator=(const DynamicClass<T>& rhs)
{
	if(this==&rhs)
	{
		return *this;
	}

	mSize=rhs.mSize;
	delete[] mData;
	mData= new T[mSize];

	for(int i=0;i<mSize;++i)
	{
		mData[i]=rhs.mData[i];
	}

	return *this;
}

template<typename T>
int DynamicClass<T>::size()
{
	return mSize;
}


template<typename T>
DynamicClass<T>::~DynamicClass()
{
	delete[] mData;
	mData=0;
}




Thanks!
The linker does not see the definition of the default constructor

DynamicClass<int>::DynamicClass();
Last edited on
Topic archived. No new replies allowed.