Hi team
I was working on template and found the linker error while compiling the code.
Below is the code:
Code:
//Template1.h: Header file where template define
#ifndef __Template1_H__
#define __Template1__
template <typename T,typename T1> class A
{
T a;
T1 b;
public:
A(T ,T1 ) ;
void getData();
};
#endif // __
Code:
//Template1.cpp where class define
#include<iostream>
#include<string>
#include "Template1.h"
using namespace std;
template<typename T,typename T1> A<T,T1>:: A(T x,T1 y)
{
a = x;
b = y;
}
template<typename T,typename T1> void A<T,T1>:: getData()
{
cout<< a << b;
}
Code:
// Main Class from where code called:Template2.cpp
#include<iostream>
#include<string>
#include "Template1.h"
int main()
{
A<int,char> f(1,'d');
f.getData();
getchar();
return 0;
}
I am getting below linker error:
Template2.obj : error LNK2001: unresolved external symbol "public: void __thiscall A<int,char>::getData(void)" (?getData@?$A@HD@@QAEXXZ)
Template2.obj : error LNK2001: unresolved external symbol "public: __thiscall A<int,char>::A<int,char>(int,char)" (??0?$A@HD@@QAE@HD@Z)
Can anyone help in this.
Thanks