X
xuatla
Hi,
I am trying to use template function and encounted some problems hope to
get your help. THANKS!
make output:
undefined reference to `int reallocateMemory<double>(double*&, int)'
( compile of auxifunc.o: OK; link: main.o auxifunc.o )
( makefile should be ok )
source code below:
==================
auxifunc.h
template < class T >
extern int reallocateMemory( T*& array, int newsize );
==================
auxifunc.cpp
template < class T >
int reallocateMemory( T*& array, int newsize )
{
delete[] array;
array = new T[newsize];
if ( !array ) return 0;
else return 1;
}
==================
main.cpp
#include "auxifunc.h"
int main()
{
double *a = new double[1];
reallocateMemory<double>( a, 10 );
return 1;
}
If I put the definition of reallocateMemory in .h file, then it's ok.
If I dont use template and put the prototype in .h and definition in
..cpp, also ok.
what's the problem in my case?
Do I need to seperate prototype and definition in two files (.h & .cpp)
instead of putting everything in .h?
When do I need to use "extern"?
Thanks a lot!
X
I am trying to use template function and encounted some problems hope to
get your help. THANKS!
make output:
undefined reference to `int reallocateMemory<double>(double*&, int)'
( compile of auxifunc.o: OK; link: main.o auxifunc.o )
( makefile should be ok )
source code below:
==================
auxifunc.h
template < class T >
extern int reallocateMemory( T*& array, int newsize );
==================
auxifunc.cpp
template < class T >
int reallocateMemory( T*& array, int newsize )
{
delete[] array;
array = new T[newsize];
if ( !array ) return 0;
else return 1;
}
==================
main.cpp
#include "auxifunc.h"
int main()
{
double *a = new double[1];
reallocateMemory<double>( a, 10 );
return 1;
}
If I put the definition of reallocateMemory in .h file, then it's ok.
If I dont use template and put the prototype in .h and definition in
..cpp, also ok.
what's the problem in my case?
Do I need to seperate prototype and definition in two files (.h & .cpp)
instead of putting everything in .h?
When do I need to use "extern"?
Thanks a lot!
X