LNK2019 error for no reason!

HI
This is my header file

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
 #ifndef _grid_h
#define _grid_h

template<typename T>
class grid{

private:
	T** main;
	int sizev;

public:

	grid();

	grid(int col, int row);

	~grid();

	void setDimensions(int col, int row);

	void set(int col, int row, T ob);

	T get(int col, int row);

	int size();
};


#endif 


and here is the interface

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
#include"grid.h"

template <typename T>
grid<T>::grid(){}

template<typename T>
grid<T>::grid(int col, int row){	
	setDimensions(col, row);
	sizev=col*row;
}

template<typename T>
grid<T>::~grid(){	
	delete[][] main;
}

template<typename T>
void grid<T>::setDimensions(int col, int row){
	main = new T*[col];
	for(int i =0;i<col;i++)
		main[i]=new T[row];
}

template<typename T>
void grid<T>::set(int col, int row, T ob){
	main[col][row] = ob;
}

template<typename T>
T grid<T>::get(int col, int row){
	return main[col][row];
}

template<typename T>
int grid<T>::size(){
	return sizev;
}


and here is the errors I get when I compile:

1
2
3
1>Source.obj : error LNK2019: unresolved external symbol "public: __thiscall grid<int>::grid<int>(int,int)" (??0?$grid@H@@QAE@HH@Z) referenced in function _main
1>Source.obj : error LNK2019: unresolved external symbol "public: __thiscall grid<int>::~grid<int>(void)" (??1?$grid@H@@QAE@XZ) referenced in function _main
1>C:\Users\yadi\documents\visual studio 2012\Projects\Grid\Debug\Grid.exe : fatal error LNK1120: 2 unresolved externals


Now we clearly see that I have defined both the constructor and destructor in the interface, why is it giving me this error?
Hi,

All the template code must generally be in the header file. There is a work around, but it is not worth it IMO.

https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl

HTH
Thank you. Thats how I made it work
Topic archived. No new replies allowed.