-
Template + Multifile
Hi friends
I wrote some program with structue like this :
but it does'nt work , and I don't know why
please help me , Thank you
main.cpp
Code:
#include "Unit10.h"
#include <conio.h>
int main(int argc, char* argv[])
{
tst<int> obj(10);
obj.show();
getch();
return 0;
}
Implement (Unit9.cpp) :
Code:
#include <iostream>
using std::cout ;
#include "Unit10.h"
template <typename T>
void tst<T>::show()
{
cout << i ;
}
and interface:
Code:
#ifndef Unit10H
#define Unit10H
template <typename T>
class tst
{
public :
T i ;
tst<T>(T n=0) : i(n) {}
void show() ;
};
#endif
error :
Code:
[Linker Error] Error: Unresolved external 'tst<int>::show()' referenced from C:\UNIT9.OBJ
and I checked it in C++Builder , dm , DevC++
but the result of all of them were similar.
-
Most compilers don't support template function defintions in a separate file (the two you've mentioned definitely don't). Move the template function definition into the header file, Unit10.h
As an aside, take a look at this faq, too.